I want to merge the same id, but it's not as easy as I thought. Can you help me?
const data = [
{id: 1, stats: {kill:7, de: 3, as: 8}, wins: true},
{id: 2, stats: {kill:1, de: 3, as: 8}, wins: false},
{id: 2, stats: {kill:17, de: 9, as: 3}, wins: true},
{id: 1, stats: {kill:6, de: 2, as: 1}, wins: true},
];
My code until now
const result = data.reduce((b,c) => ((b[b.findIndex(d => d.id === c.id)] || b[b.push({
id: c.id,
stats: c.stats.kill,
count: 0,
}) - 1]).count++, b), []);
goal
[
{id: 1, stats: {kill: 13, de: 5, as: 9}, victoryRate: '100%', count:2},
{id: 2, stats: {kill: 18, de: 12, as: 11}, victoryRate: '50%', count: 2}
]
If it is difficult to answer, please recommend Keyword so that I can do it myself!
Thanks