Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Checking for same values and update array

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading