How to grouped json and sum its score? i have make this approach but still stuck to addition the value.
I have this json value:
A = [ { name: 'Boy', score: 10}, { name: 'Marry', score: 90 }, { name: 'Boy', score: 70 } ]
i want to get the result json like this
Result = [ { name: 'Boy', score: 80 }, { name: 'Marry', score: 90 } ]
i tried this code but still failed to add the value
let grouped = Object.values(data.reduce((r, {nickname, score}) => {
r[nickname] = r[nickname] || { nickname, score: [] };
r[nickname].score = r[nickname].score + score
return r;
}, {}))
console.log(grouped);
the result is
Result = [ { name: 'Boy', score: '1070' }, { name: 'Marry', score: '90' } ]
it converted to string :( Please help me