I have an array of objects:
[{person:101, year: 2012}, {person:102, year: 2012}, {person:103, year: 2013}]
And I want to be able to return an
You need to use simple for like below :
var data = [{person:101, year: 2011},
{person:102, year: 2012},
{person:103, year: 2011}];
var result = {},
i;
for(i=0; i < data.length; i++){
let year = data[i].year;
if(typeof result[year] === 'undefined'){
result[year] = 1;
}
else{
result[year] = result[year] + 1;
}
}
console.log(result);