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

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

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