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

245
Views
Merge 2 arrays without overwrite

I want merge 2 arrays with the same fields by gkey but I don't want to override the first array felids I'm using map() and find() but it overwrites the first array and undefined row

here is my code

let x = [{id:1 , name: "hi", gkey: 6,np:1000, op:0}
  ,{id:2 , name: "hello", gkey: 7,np:5000, op:0}
  ,{id:3 , name: "h", gkey: 8,np:3000, op:0}];

let y = [{id:11,name: "hi" ,gkey:6,np:0,op:100},{id:22,name:"hi",gkey:8,np:0,op:800},];

var marr = x.map(i => y.find(s => s.gkey === i.gkey))

console.log(marr);

here is my result: enter image description here

i want my merge data result like this:

{id:1 , name: "hi", gkey: 6,np:1000, op:100}

{id:2 , name: "hello", gkey: 7,np:5000, op:0}

{id:3 , name: "h", gkey: 8,np:3000, op:800}

any solutions?

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

0

Your code is almost there, but have to create and return a merged object:

let x = [{id:1 , name: "hi", gkey: 6,np:1000, op:0}
  ,{id:2 , name: "hello", gkey: 7,np:5000, op:0}
  ,{id:3 , name: "h", gkey: 8,np:3000, op:0}];

let y = [{id:11,name: "hi" ,gkey:6,np:0,op:100},{id:22,name:"hi",gkey:8,np:0,op:800},];

var marr = x.map(elX => {
  const elY = y.find(s => s.gkey === elX.gkey);
  return { ...elX, np: elX.np + (elY?.np ?? 0), op: elX.op + (elY?.op ?? 0) };
  });

console.log(marr);

about 4 years ago · Juan Pablo Isaza Report

0

Other solutions are just fine, but I was wondering what if the operation is truly a merge one, and the y array contain more items than x.

let x = [
  { id: 1, name: "hi", gkey: 6, np: 1000, op: 0 },
  { id: 2, name: "hello", gkey: 7, np: 5000, op: 0 },
  { id: 3, name: "h", gkey: 8, np: 3000, op: 0 },
];

let y = [
  { id: 11, name: "hi", gkey: 6, np: 0, op: 100 },
  { id: 22, name: "hi", gkey: 8, np: 0, op: 800 },
  { id: 33, name: "new entry in Y arr", gkey: 8, np: 0, op: 5545 },
];

let result = x.concat(y)
              .reduce((acc, current) => 
              {
                let found = acc.find((i) => i["gkey"] === current["gkey"]);
                if (found) {
                  found.op = current["op"];
                  return acc;
                }
                acc.push(current);
                return acc;
              }, []);

console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

insert the elements of y into x individually , like code below :

let x = [{id:1 , name: "hi", gkey: 6,np:1000, op:0}
  ,{id:2 , name: "hello", gkey: 7,np:5000, op:0}
  ,{id:3 , name: "h", gkey: 8,np:3000, op:0}];

let y = [{id:11,name: "hi" ,gkey:6,np:0,op:100},{id:22,name:"hi",gkey:8,np:0,op:800},];

y.forEach(element => {
    x.push(element)

});

console.log(x);

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!