I am working with Nodejs and I am stuck on this given below problem and looking for best practices to do it.
I have an array set that would be like this:
[
{ id: 'something1', score: 180, rank: 1, avatar: 'avatar_3', prize: 1280 },
{ id: 'something2', score: 180, rank: 2, avatar: 'avatar_3', prize: 1073 },
{ id: 'something3', score: 161, rank: 3, avatar: 'avatar_2', prize: 902 },
{ id: 'something4', score: 161, rank: 4, avatar: 'avatar_7', prize: 758 },
{ id: 'something5', score: 161, rank: 5, avatar: 'avatar_8', prize: 535 },
{ id: 'something6', score: 161, rank: 6, avatar: 'avatar_3', prize: 318 },
{ id: 'something7', score: 143, rank: 7, avatar: 'avatar_3', prize: 267 },
{ id: 'something8', score: 143, rank: 8, avatar: 'avatar_4', prize: 224 },
{ id: 'something9', score: 140, rank: 9, avatar: 'avatar_3', prize: 210 }
]
and now I want to add all those prizes that have identical scores and then divide them by total players having the same score and then updating the existing array with new prize values.
Here is the sample output that I am looking for:
[
{ id: 'something1', score: 180, rank: 1, avatar: 'avatar_3', prize: 1176 },
{ id: 'something2', score: 180, rank: 2, avatar: 'avatar_3', prize: 1176 },
{ id: 'something3', score: 161, rank: 3, avatar: 'avatar_2', prize: 628},
{ id: 'something4', score: 161, rank: 4, avatar: 'avatar_7', prize: 628},
{ id: 'something5', score: 161, rank: 5, avatar: 'avatar_8', prize: 628},
{ id: 'something6', score: 161, rank: 6, avatar: 'avatar_3', prize: 628},
{ id: 'something7', score: 143, rank: 7, avatar: 'avatar_3', prize: 245},
{ id: 'something8', score: 143, rank: 8, avatar: 'avatar_4', prize: 245},
{ id: 'something9', score: 140, rank: 9, avatar: 'avatar_3', prize: 210 }
]
So what would be the best way to solve this problem? Thanks
CodePudding user response:
Build a hash table of totals first (I've used a for...of
loop), and then map
over the data and replace the prize
value of each object with the total that matches the total
value in the hash table for that score.
const data=[{id:"something1",score:180,rank:1,avatar:"avatar_3",prize:1176},{id:"something2",score:180,rank:2,avatar:"avatar_3",prize:1176},{id:"something3",score:161,rank:3,avatar:"avatar_2",prize:628},{id:"something4",score:161,rank:4,avatar:"avatar_7",prize:628},{id:"something5",score:161,rank:5,avatar:"avatar_8",prize:628},{id:"something6",score:161,rank:6,avatar:"avatar_3",prize:628},{id:"something7",score:143,rank:7,avatar:"avatar_3",prize:245},{id:"something8",score:143,rank:8,avatar:"avatar_4",prize:245},{id:"something9",score:140,rank:9,avatar:"avatar_3",prize:210}];
const out = {};
// Iterate over the data
for (const obj of data) {
// Destructure the score an prize from each object
const { score, prize } = obj;
// Initialise the key/value of an `out` property
// with an object with default values
out[score] ??= { players: 0, prizes: 0 };
// Increase the players count
out[score].players;
// Add the prize of the current object to
// the `prizes` value
out[score].prizes = obj.prize;
// Calculate the current score
out[score].total = out[score].prizes / out[score].players;
}
// Return a new array of objects updating each
// score with the `total` value from the hash object
const result = data.map(obj => {
return { ...obj, prize: out[obj.score].total };
});
console.log(result);
CodePudding user response:
This should do it
const computeAveragePrizes = (arr) => {
const grouped = arr.reduce((acc , cur) => {
const existing = acc[cur.score];
if (existing) {
return {
...acc ,
[cur.score]: {
total: existing.total cur.prize ,
count: existing.count 1
}
}
}
return {
...acc ,
[cur.score]: {
total: cur.prize ,
count: 1
}
}
} , {}
);
return arr.map(item => ({
...item,
prize: grouped[item.score].total / grouped[item.score].count
}))
}