The problem was:
const arr = [
{ name1: 'value1', qty: 1, time: 1 },
{ name1: 'value2', qty: 1, time: 2 },
];
// using this reducer works!
const reducer = (acc, { name1, qty}) => {
const newQty = parseInt(acc[name1] ?? 0) + parseInt(qty); //?
return {
...acc,
[name1]: newQty
};
};
arr.reduce(reducer, {})
// this returns: {value1: sum, value2: sum, ... }
So far so good... but what happens when you have this?
const arr2 = [
{ name2: 'valueX', qty: 1, time: 1},
{ name2: 'valueY', qty: 1, time: 2},
];
Copy pasting and then changing the name works fine, of course... but is there another way?