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

235
Vistas
How to compare Object and array in Javascript?

In my application I am trying to compared ids of specification and selectedSpecifications. As of now I hardcoded selectedSpecifications id to 0. I am trying to loop over selectedSpecifications array and then check if id matches.

selectedSpecifications  is an array
specification  is an object

I need help to achieve this functionality.

 let val = this.selectedSpecifications.map(specification => {
        
   if (specification.id == this.selectedSpecifications[0].id) {  ----> How can I remove
 hardcoded value `0` and loop over selectedSpecifications array

        return specification.value ;
}
else{
       
}}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The main point of clarification is the syntax for map and find is arr.map(eachElementInArray => doSomethingWithElement), but it seems like you're trying to reference the specification as an argument (the other way around). Instead, you want something like this:

let val = this.selectedSpecifications.find(selectedSpec => {
   if (selectedSpec.id === specification.id) {
        return selectedSpec.value;
}

If there's only 1 or no matching specification, I would use Array.prototype.find(). Otherwise map() makes sense if there are multiple matching specifications.

As a final note, I would rather use the strict equality operator (===) here just in case the saved id's have different types, which might help you catch a bug.

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