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

149
Views
Merge nested array of objects by comparing key
  • I have an 3 nested array of Objects and I need to merge them all in single array of objects by comparing key. and if any object have same key then their inner object will merge. And if key is not same then also the nested array of objects will merge*
var obj1 = {    
    a: {
    c: 3
  }
}

var obj2 = {    
    b: {
    e: 40
  }
}

var obj3 = {    
    d: {
    x: 30
  },
  a: {
    f: 66
  }
}

// The expected output will be like this - 

// 
/*OUTPUT::: 
{
    a: {
    c: 3,
    f: 66
  },
  b: {
    e: 40
  },
  d: {
    x: 30
  }
}

Please tell me which approach would be suitable ?
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can group the objects using Array.prototype.reduce.

const 
  obj1 = { a: { c: 3 } },
  obj2 = { b: { e: 40 } },
  obj3 = { d: { x: 30 }, a: { f: 66 } };

const res = [obj1, obj2, obj3].reduce((r, o) => {
  Object.entries(o).forEach(([k, v]) => {
    r[k] = { ...r[k], ...v };
  });
  return r;
}, {});

console.log(res);

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!