Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

250
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda