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

197
Visualizações
Nodejs - How to find the index with two conditions, one condition being where a property exists

I have an object that looks like this:

{
   "examples":[
      {
         "key":"value1"
      },
      {
         "key":"value1",
         "key2":"example"
      },
      {
         "key":"value1"
      },
      {
         "key":"value2"
      },
      {
         "key":"value2",
         "key2":"example"
      },
      {
         "key":"value2"
      }
   ]
}

I'm trying to use findIndex to find where in the object that key === 'value1' and key2 exists (so in this instance the index would be 1). I've tried using something like var x = examples.findIndex(({key}) => key === 'value1' && ({key2}) => key2) but it's not working. How do I go about this? Any answers would be greatly appreciated! :)

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

examples is a property of the data object, so you need to iterate over that array. examples.findIndex won't do anything.

You can then check to see if key has "value1" as a value, and that there is a key2 in the object.

const data={examples:[{key:"value1"},{key:"value1",key2:"example"},{key:"value1"},{key:"value2"},{key:"value2",key2:"example"},{key:"value2"}]};

const result = data.examples.findIndex(obj => {
  return obj.key === 'value1' && obj.key2;
});

console.log(result);

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