1

I want to loop through a json object and count elements with the same value in different arrays. Here's a sample of my object

var testconn =
{"_total": 3,
    "values":
    [
        {
            "articlesRead": [
                { "articleId": 1001 },
                { "articleId": 1002 },
                { "articleId": 1003 },
                { "articleId": 1004 },
                { "articleId": 1005 },
                { "articleId": 1006 }
            ]
        },
        {
            "articlesRead": [
                { "articleId": 1001 },
                { "articleId": 1002 },
                { "articleId": 2001 },
                { "articleId": 2002 },
                { "articleId": 2003 },
                { "articleId": 2004 }
            ]
        },
        {
            "articlesRead": [
                { "articleId": 1001 },
                { "articleId": 3001 },
                { "articleId": 3002 },
                { "articleId": 3003 },
                { "articleId": 3004 },
                { "articleId": 3005 }
            ]
        }
    ]
}

I want to count how many the ID's exsist in the array. So for this example I shoud have: 1001 x 3 1002 x 2 1003 x 1 1004 x 1 2001 x 1 etc

I'm looping through my object like this

for (i = 0; i < testconn.values.length; i++) {
    var c = testconn.values[i];
    for (j = 0; j < c.articlesRead.length; j++) {
        var a = c.articlesRead[j];
        for (var key in a) {
            if (a[key] is bigger then 1) {
                count a[key]
            }
            return number of

        }
    }
}
0

2 Answers 2

3

Just use an object to store the value as a key and increment it:

var id={};
for (var i = 0; i < testconn._total; i++) {
    var c = testconn.values[i];
    for (j = 0; j < c.articlesRead.length; j++) {
        var a = c.articlesRead[j];
        id[a.articleId]= id[a.articleId] ? ++id[a.articleId]: 1;
    }
}
console.log(id);
Sign up to request clarification or add additional context in comments.

2 Comments

Excellent, this is what I was looking for (Is it to much to ask to note the if statement in its regular form? I'm not as advanced and would like to understand what you did)
I am using a ternary operator, it makes things shorter, it's the same as:if(id[a.articleId]) { ...} else {...}
0

Here's an easy way to go about it, using 4 nested for loops, maybe not the most efficient though.

var list = testconn.values;
var dict = {};  


for(var i in list){
    for(var j in list[i]){
        for(var k in list[i][j]){
            for(var l in list[i][j][k]){
                var key = list[i][j][k][l];
                dict[key] = dict[key] ? ++dict[key] : 1;
            }
        }
    }
}

Dict is Now an associative array of what you want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.