ankit pachori ankit pachori - 2 months ago 10
Javascript Question

how to count similar value object in json?

I have this JSON structure:

[{
"name": "ankit",
"DOB": "23/06"
}, {
"name": "kapil",
"DOB": "26/06"
}, {
"name": "ankit",
"DOB": "27/06"
}]


I want to count similar object with value
ankit
. How can I do this?

Answer

How about:

let a = [
    { "name": "ankit", "DOB": "23/06" }, 
    { "name": "kapil", "DOB": "26/06" }, 
    { "name": "ankit", "DOB": "27/06" }
];

let count = 0;
a.forEach(item => {
    if (item.name === "ankit") {
        count++;
    }
});

(code in playground)

Comments