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

100
Views
Add objects of two different Arrays

I have two arrays and each of them has objects. How best can I simplify adding two objects into one but in a new list. e.g

a = [{a:1, b:2, c:3}, {d:1, e:4, f:2}]
b = [{m:1, n:2, o:4}, {r:1,s:3,u:5}, {k:1,j:4,f:8}]

z = [{a:1, b:2, c:3, m:1, n:2, o:4}, {d:1, e:4, f:2, r:1,s:3,u:5}, {k:1,j:4,f:8}]

Suppose you have list a and b, I want to add the objects of each position together in list z.

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

0

You could merge the two object in this way:

let a = [{a:1, b:2, c:3}, {d:1, e:4, f:2}]
let b = [{m:1, n:2, o:4}, {r:1,s:3,u:5}, {k:1,j:4,f:8}]

let z = [];

b.forEach((x, i) => {
     let merged = {...x, ...a[i]};
     z.push(merged)
})

console.log(z);

about 4 years ago · Juan Pablo Isaza Report

0

I'd go over the maximum length of a and b and use Object.assign to copy the values. Assuming you want a generic solution to any two arrays, where either can be longer than the other, note that you need to check the lengths as you go:

z = [];
for (let i = 0; i < Math.max(a.length, b.length); ++i) {
    const result = i < a.length ? a[i] : {};
    Object.assign(result, i < b.length ? b[i] : {});
    z.push(result);
}
about 4 years ago · Juan Pablo Isaza Report

0

Try this :

const a = [{a:1, b:2, c:3}, {d:1, e:4, f:2}];
const b = [{m:1, n:2, o:4}, {r:1,s:3,u:5}, {k:1,j:4,f:8}];
let c = [];

if(a.length > b.length){
    c = a.map((e,i) => {
     return {
        ...e,
        ...b[i]
     }
   });
}else{
  c = b.map((e,i) => {
      return {
        ...e,
        ...a[i]
     }
  });
}
console.log(c);

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!