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

123
Views
Get an object from an array of objects with the highest value of a property

I have an array of objects that looks like this:

Array(3) [ {…}, {…}, {…} ]

0: Object { plName: "player1", plNumber: 1, score: 30, … }
1: Object { plName: "player2", plNumber: 2, score: 24, … }
2: Object { plName: "player3", plNumber: 3, score: 89, … }

I'm trying to figure out the best way to get an object with the highest value of the score property. In this case:

2: Object { plName: "player3", plNumber: 3, score: 89, … }

I would really appreciate any help :)

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

A simple reduce should do it.

let arr = [{
    plName: "player1",
    plNumber: 1,
    score: 30
  },
  {
    plName: "player2",
    plNumber: 2,
    score: 24
  },
  {
    plName: "player3",
    plNumber: 3,
    score: 89
  }
];

let result = arr.reduce((o1, o2) => o1.score > o2.score ? o1 : o2);
console.log(result);

If there could be multiple high score

let arr = [{
    plName: "player1",
    plNumber: 1,
    score: 30
  },
  {
    plName: "player2",
    plNumber: 2,
    score: 24
  },
  {
    plName: "player3",
    plNumber: 3,
    score: 89
  },
  {
    plName: "player4",
    plNumber: 4,
    score: 24
  },
  {
    plName: "player5",
    plNumber: 5,
    score: 89
  }
];

let maxScoreObject = arr.reduce((o1, o2) => o1.score > o2.score ? o1 : o2);
let result = arr.filter(o => o.score === maxScoreObject.score);
console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

A generic function could be something like

const data = [{
    plName: "player1",
    plNumber: 1,
    score: 30
  },
  {
    plName: "player2",
    plNumber: 2,
    score: 24
  },
  {
    plName: "player3",
    plNumber: 3,
    score: 89
  }
];


function maxBy(array, property) {
  return array.reduce((max, item) => {
    if (item[property] > max[property]) return item;
    return max;
  })
}

console.log(maxBy(data, 'score'));

about 4 years ago · Juan Pablo Isaza Report

0

  • Sort the objects in descending order from biggest to smallest score, get the first one which is the maximum score.
data.sort((a,b)=>b.score-a.score)[0]
//{plName: 'player3', plNumber: 3, score: 89}
about 4 years ago · Juan Pablo Isaza 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!