Struggling lately to tally the duplicate elements in an array and convert them into objects. Currently, I am attempting to calculate the frequencies of age ranges. Here is a code snippet that counts duplicate ages:
const age = [5, 5, 16, 5, 16];
const sumAge = {};
for (const datum of age) {
let entry = sumAge[datum];
if (entry) {
++entry.count;
} else {
sumAge[datum] = {age: datum, count: 1};
}
}
console.log(Object.values(sumAge));
This results in
{
age:5,
value: 3
},
{
age:16,
value: 2
}
Currently tackling the task of achieving this expected outcome
{
age:5-15,
value: 3
},
{
age:16-30,
value: 2
}