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

251
Vistas
Why does this array.prototype.find does not work?

I have the following object

const someobj = {constantValues: {status: [{value: 1, key : "enabled"},{value: 0, key: "disabled"},{value: 3, key: "archived"}]}}

And the following key

const keyFilter = "status"

I try to filter the object with the value 3 from someobj, so i do

const constantValueIsNumber = typeof someobj.constantValues.status[0].value === "number" ? true: false
const val = someobj.constantValues[keyFilter].find(i => i.value === constantValueIsNumber? Number("3"): "3")

console.log(val) // {value: 1, key: "enabled"}

WHy is it returning 1, when im filtering 3 and it should return

{value: 3, key: "archived"}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

This works for me.

const someobj = {constantValues: {status: [{value: 1, key : "enabled"},{value: 0, key: "disabled"},{value: 3, key: "archived"}]}};

const keyFilter = "status";
const valueToCheck = "3";

const val = someobj.constantValues[keyFilter].find(i => i.value.toString() === valueToCheck.toString());

console.log(val);

We convert all values to string so both numbers and strings work.

about 4 years ago · Juan Pablo Isaza Denunciar

0

your predicate was not written correctly; you should place parenthesis to define the order of operations. The .find() method has to be something like this

const val = someobj.constantValues[keyFilter].find(i => i.value === (constantValueIsNumber? 3 : "3"))

it outputs: val: {value: 3, key: 'archived'}

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