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

108
Views
Take an object from an array of objects based on the max value on one of its properties

Let's say I got an array of object like this

const arr = [
  {name: 'John', age: 15},
  {name: 'Max', age: 17},
  {name: 'Tom', age: 11},
]

How can I take just the object containing Max, 17? This would be the result

b = [{ name: Max, age: 17 }]

or better

c = { name: Max, age: 17 }
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Reduce the array to the object with highest age:

const arr = [{"name":"John","age":15},{"name":"Max","age":17},{"name":"Tom","age":11}]

const result = arr.reduce((acc, o) => !acc || o.age > acc.age ? o : acc, null)

console.log(result)

I'm using null as the default value for the Array.reduce() to prevent an error if the array is empty. However, you can check for an empty array beforehand as well:

const findMaxAge = arr => arr.length 
  ? arr.reduce((acc, o) => o.age > acc.age ? o : acc)
  : null

const arr = [{"name":"John","age":15},{"name":"Max","age":17},{"name":"Tom","age":11}]

console.log(findMaxAge(arr))

console.log(findMaxAge([]))

about 4 years ago · Juan Pablo Isaza Report

0

You can sort by age first, then the object with the highest age value will be at the start of the list:

const a = [
  {name: "John", age: 15},
  {name: "Max", age: 17},
  {name: "Tom", age: 11},
]

const sortedByAge = a.sort((a,b) => b.age - a.age);

const highestAge = sortedByAge[0];

console.log(highestAge);
about 4 years ago · Juan Pablo Isaza Report

0

hope this code helping you

const a = [{name: 'John', age: 15},
       {name: 'Max', age: 17},
       {name: 'Tom', age: 11},];
const result = a.reduce((p, c) => p.age > c.age ? p : c);
console.log(result);
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!