Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

93
Views
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.

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

about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

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);

about 4 years ago · Santiago Gelvez Report

0

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
  }))
}
about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!