[Solved] How to count people in JSON file? [closed]


You should be able to do this pretty easily using Array.map() and Array.filter(). Here’s an example

var users = [
    {
        "id": 1,
        "username": "Michael",
        "users": [
            {
                "id": 2,
                "like": 1
            },
            {
                "id": 3,
                "like": 1
            },
            {
                "id": 4,
                "like": 0
            },
            {
                "id": 5,
                "like": 1
            }
        ]
    },
    {
        "id": 2,
        "username": "Antony",
        "users": [
            {
                "id": 1,
                "like": 0
            },
            {
                "id": 3,
                "like": 1
            },
            {
                "id": 4,
                "like": 0
            },
            {
                "id": 5,
                "like": 1
            }
        ]
    }
];

var usersWithLikes = users.map(function(user) {
    return user.users.filter(function(value) {
        return value.like === 1;    
    }).length;
});

console.log(usersWithLikes);

1

solved How to count people in JSON file? [closed]