How to count number of values in array?

江枫思渺然 提交于 2019-12-13 04:27:03

问题


I am trying to count the values a json array.

I want to count the number of id's in the data array of "sierra" if "beta = b". Hence checking the value of data[].beta against the environment variable ("B") set to value 'b'.

The issue here is I do not have "sierra" in every iteration of data[].

{
  "data": [{
        "alpha": "a",
        "beta": "b",
        "delta": {
            "cat": "dog"
        },
        "gamma": {
            "sierra": {
                "data": [
                    {
                        "type": "alphabet",
                        "id": "a"
                    },
                    {
                        "type": "alphabet",
                        "id": "b"
                    }
                ]
            }
        }
   },
{
        "alpha": "a",
        "beta": "b",
        "delta": {
            "cat": "dog"
        },
        "bravo": {
                "data": [
                    {
                        "type": "number",
                        "id": "1"
                    },
                    {
                        "type": "number",
                        "id": "2"
                    }
                ]
            }
        }
   },
{
        "alpha": "x",
        "beta": "y",
        "delta": {
            "cat": "dog"
        },
        "gamma": {
            "sierra": {
                "data": [
                    {
                        "type": "alphabet",
                        "id": "c"
                    },
                    {
                        "type": "alphabet",
                        "id": "d"
                    }
                ]
            }
        }
   }]
}

Above json is the response body I see in postman. "loop" is the count of my "for" loop.

EDIT 1: I've tried this:

1. pm.response.json().data[loop].gamma.sierra.data().id).size()

2. for(var loop =0; loop < pm.response.json().data.length; loop++)
{
 if(pm.response.json().data[loop].beta===pm.variables.get("B"))
{      
        pm.response.json().data.map((item, loop) => {
            if(item.gamma){ // check if gamma key is present
                console.log(item.gamma.sierra.filter(data =>data.id 
                                                      ).length); // 
            }
        });
        result=true;
        break;

    }
}
pm.expect(true).to.eql(result);

Expected: 2

Actual: TypeError: Cannot read property 'sierra' of undefined
Actual: item.relationships.apps.filter is not a function

回答1:


You could take a dynamic apporach and hand over the key of the object where you like to count a certain inner key.

function count(object, key, subKey) {
    const noObject = o => !o || typeof o !== 'object';

    function subCount(object) {
        if (noObject(object)) return 0;
        if (subKey in object) return 1;
        return Object.values(object).reduce((s, o) => s + subCount(o), 0);
    }

    if (noObject(object)) return 0;
    if (key in object) return subCount(object[key]);
    return Object.values(object).reduce((s, o) => s + count(o, key, subKey), 0);
}

var data = { data: [{ alpha: "a", beta: "b", delta: { cat: "dog" }, gamma: { sierra: { data: [{ type: "alphabet", id: "a" }, { type: "alphabet", id: "b" }] } } }] };

console.log(count(data, 'sierra', 'id')); // 2



回答2:


You can access it like this. If you have multiple data records you can use each loop to calculate as well.

a = {
 "data": [ 
 {
        "alpha": "a",
        "beta": "b",
        "delta": {
            "cat": "dog"
        },
        "gamma": {
            "sierra": {
                "data": [
                    {
                        "type": "alphabet",
                        "id": "a"
                    },
                    {
                        "type": "alphabet",
                        "id": "b"
                    }
                ]
            }
        }
    }
]
}

console.log(a.data[0].gamma.sierra.data.length);



回答3:


You can use below code:

pm.response.json().data[0].gamma.sierra.data.filter( d => d.id ).length

Hope it helps.




回答4:


pm.response.json().data.map((item, loop) => {
    if(item.beta === "b" && item.gamma){ // check if gamma key is present

        console.log(item.gamma.sierra.data.filter(data => data.id).length); // 
    }
});

Jsfiddle



来源:https://stackoverflow.com/questions/56785331/how-to-count-number-of-values-in-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!