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

104
Views
Compare array with object key/value by specific value in javascript

I need to create a new object key/value. In it i need the values from the already existing key/value object and i need to add values that are only in the array.

The array:

[{ name: "Computer", name: "Car", name: "House", name: "Flower"}]

The object key/value:

{ computer: { title: 'Computer', color: 'Black' }, house: { title: 'House', color: 'Red' }}

So in this case the new object would need to be:

{ computer: { title: 'Computer', color: 'Black' }, car: { title: 'Car', color:'' }, house: { title: 'House', color: 'Red' }, flower: { title: 'Flower', color:'' } } 

What is the easiest way to achieve this? I was thinking to loop over the array compare the values between them and extract the repeating ones, but i'm not sure how to do that.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I've made the assumption that that first array was meant to be an array of individual objects each with a key of 'name' as in the arr array.

const arr = [ {name: "Computer"}, {name: "Car"}, {name: "House"}, {name: "Flower"}];
const objVal = { 
        computer: { title: 'Computer', color: 'Black' }, 
        house: { title: 'House', color: 'Red' }
        };
const expected = { 
        computer: { title: 'Computer', color: 'Black' }, 
        car: { title: 'Car', color:'' }, 
        house: { title: 'House', color: 'Red' }, 
        flower: { title: 'Flower', color:'' } 
        } 

let returnObj = {};

arr.forEach((element) => {
    const name = element.name;
    if (objVal[name.toLowerCase()]) {
        returnObj[name.toLowerCase()] = objVal[name.toLowerCase()];
    } else {
      returnObj[name.toLowerCase()] = {
        title: name,
        color: ''
      }
    }
});

console.log(returnObj);
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!