My data:
const data = [{
"id": 1,
"location": 30,
"year": 2019
},
{
"id": 2,
"location": 15,
"year": 2000
},
{
"id": 1,
"location": 25,
"year": 2019
}
];
My desired outcome:
accumluatedData = [{
"id": [1],
"year": 2019,
"frequency": 2
},
{
"id": [2],
"year": 2000,
"frequency": 1
}
];
What I have so far (with the kind help of other stackoverflowers), is getting into an array (see code below).
However, I would like to get it into an object (as shown above).
Any suggestions? Am I on the right track? Or should I use a different route?
const data = [{
"id": 1,
"location": 30,
"year": 2019
},
{
"id": 2,
"location": 15,
"year": 2000
},
{
"id": 1,
"location": 25,
"year": 2019
}
];
var results = [];
for (var i = 0; i < data.length; ++i) {
if (results[data[i].year]) {
results[data[i].year]++;
} else {
results[data[i].year] = 1;
}
};
console.log("results", results);