I want to return an array grouped by the team with gp, win, loss summed up. I'm trying to accomplish this with reduce, however, the totals are not adding up. Here's my code...
const myArr = [
{team: 'Red', gp: 3, win:2, loss:1},
{team: 'Black', gp: 3, win:1, loss:2},
{team: 'Red', gp: 10, win:8, loss:2}
]
let output = myArr.reduce(
(acc, curr) => {
acc[curr.team] = {
gp: acc.gp + curr.gp,
win: acc.win + curr.win,
loss: acc.loss + curr.loss
};
return acc;
}, {
gp: 0,
win: 0,
loss: 0
}
);
console.log(output);
This code returns the array in the format I need, however, the gp, win, loss is not summed up, instead it shows the last data point.