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

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

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 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