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

JS Compare two objects and return new key saying who has the highest value

I have one array of objects:

teamScores: [
    { teamName: 'Team name 1', points: 21},
    { teamName: 'Team name 2', points: 11},
  ],

I need to compare the key ‘points’ within these two objects and return a new key saying if its the highest value. For this example the return should be:

teamScores: [
    { teamName: 'Team name 1', points: 21, isHighest: true},
    { teamName: 'Team name 2', points: 11, isHighest: false},
  ],

And vice versa:

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

teamScores: [
    { teamName: 'Team name 1', points: 11, isHighest: false},
    { teamName: 'Team name 2', points: 21, isHighest: true},
  ],

I tried to use .reduce() but it didn’t work so well. Here is my try:

const newTeams = scoreTeams.reduce((acc, team, idx) => {
  const isHigh = scoreTeams[idx].points > scoreTeams[idx - 1]?.points;
  const accTeam = {
    ...team,
    isHighest: isHigh,
  };
  acc[idx] = accTeam;
  return acc;
}, []);

If the first object has a lower value it works well, the output:

teamScores: [
    { teamName: 'Team name 1', points: 11, isHighest: false},
    { teamName: 'Team name 2', points: 21, isHighest: true},
  ],

But if the first value is higher than the second, it doesn’t work so well, the output:

teamScores: [
    { teamName: 'Team name 1', points: 21, isHighest: false},
    { teamName: 'Team name 2', points: 11, isHighest: false},
  ],

I need help to compare these objects properly, thanks in advance!

>Solution :

  1. map over each object to get an array of the all the points.
  2. Find the highest value of that array. (Math.max finds the highest value from a list of values. Because we’re passing it an array we have to spread the values out).
  3. Return a new array of updated objects where isHighest is set to true or false if the object’s points value matches the highest value.
const scores = [
  { teamName: 'Team name 1', points: 21 },
  { teamName: 'Team name 2', points: 11 }
];

// Collate the points
const points = scores.map(obj => obj.points);

// Find the highest point
const highest = Math.max(...points);

// `spread` out the initial object into a new object
// and add the new `isHighest` value
const out = scores.map(obj => {
  return { ...obj, isHighest: obj.points === highest };
});

console.log(out);
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