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

173
Vistas
Extract the last item which matches the condition

I have an array of objects, where I am checking for a boolean property, I need to get the last one from the array of objects which is true and need to get the name property.

This is what I have done, is there any better approach or simple way to achieve this:

const a = [{is_done: true, name: 'a'}, {is_done: true, name: 'b'}, {is_done: false, name: 'c'}]


let output = a.filter(a => a.is_done).slice(-1)[0].name


console.log(output)

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can achieve this by first applying reverse and then using find which will return the first item which corresponds to the condition you specify.

Please note that you would probably want to apply the reverse method on a copy of a string and not on the original so it's better to use [...a] as hjrshng commented below.

const a = [{is_done: true, name: 'a'}, {is_done: true, name: 'b'}, {is_done: false, name: 'c'}]

const res = [...a].reverse().find(x => x.is_done);

console.log(res.name)

Please also note that in the near future, you will be able to use findLast and findLastIndex which are currently in stage 3 - https://github.com/tc39/proposals

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