I have an array where the objects are generated from a push that is inside a function, when I try to view the objects directly in the array I am successful, but I am using forEach to add the number of times that an id uses the service, but the result always returns empty.
client.onMessage(async message => {
count_commands.push({id:parseInt(regNumberPhone), age: 1});
});
const count_commands = [],
hash = Object.create(null),
result = [];
count_commands.forEach(function (o) {
if (!hash[o.id]) {
hash[o.id] = { id: o.id, age: 0 };
result.push(hash[o.id]);
}
hash[o.id].age += +o.age;
});
to look de objects in count_commands
console.log(count_commands);
Return:
[ { id: 559892099500, age: 1 },
{ id: 559892099500, age: 1 },
{ id: 559892099500, age: 1 } ]
but to see the total sum of each id the array returns empty
console.log(result);
Return:
{}
I need to return like:
[ { id: 559892099500, age: 3 } }