简体   繁体   中英

How to count number of values in array?

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

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); 

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 

You can use below code:

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

Hope it helps.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM