javascript count occurrences in array of objects code example
Example 1: array reduce and count based on proeperty js
const array = [
{id: 12, name: 'toto'},
{id: 12, name: 'toto'},
{id: 42, name: 'tutu'},
{id: 12, name: 'toto'},
];
const id = 12;
const count = array.filter((obj) => obj.id === id).length;
console.log(count);
Example 2: javascript count number of occurrences in array of objects
var result = array.reduce( (acc, o) => (acc[o.name] = (acc[o.name] || 0)+1, acc), {} );
Example 3: javascript count number of occurrences in array of objects
var array =
[
{"name":"abc","age":20}
{"name":"abc","age":20}
{"name":"abc","age":20}
{"name":"xyz","age":21}
{"name":"xyz","age":21}
]