If I have an array of object like this one:
obj = [{id:1,value:2},{id:1,value:3},{id:2,value:8}]
How can I sum the values with the same id and return another array with the totals of each index?
You can use reduce to achieve that
const obj = [{
id: 1,
value: 2
}, {
id: 1,
value: 3
}, {
id: 2,
value: 8
}, {
id: 3,
value: 4
}, {
id: 2,
value: 1
}]
const result = Object.values(
obj.reduce((a, { id, value }) => (((a[id] ??= { id, value: 0 }).value += value), a), {})
);
console.log(result)