Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

232
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!