Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

109
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda