I have an array of objects and I'm trying to combine like keys and add the values. So X should be 0, Y should be 1, and B should be 3. Thanks for any help!!!!
const arr = [{X: -1}, {Y: 1}, {X: -4}, {B: 3}, {X: 5}];
let result = {};
for (let i = 0; i < arr.length; i++) {
var item = arr[i];
for (var key in item) {
if (!(key in result))
parseInt(item);
result[key] = [];
result[key] += item[key];
}
}
console.log(result);
I expected X to be 0 but instead it is returning 5.
result[key] = [];you reset it every time. Idea is right.