Attempted two different methods to obtain a partial summary within each array object, but unfortunately, both were unsuccessful.
var arr = [
{ "value": 10, "newBalance": 0 },
{ "value": -10, "newBalance": 0 },
{ "value": 15, "newBalance": 0 },
];
let total = 0;
for (let i = 0, l = arr.length; i < l; ++i) {
total = total + arr[i].value;
arr.map(item => { item.newBalance = total; return item; });
// updating all newBalance values with the final total
arr.map(item => item.newBalance != 0 ? { ...item, newBalance: total } : item);
// does not correctly update newBalance
}
console.log(arr);
What mistake am I making in this code?