3

I am trying to merge together the array1 and array2 by matching on the sku. But if a sku has multiple (like 62617802), I want to keep both value from array1 and array2, like, first sku (62617802) will merge with first sku (62617802) of array2 and so on.

Input Note: If the SKU is duplicate (like twice or thrice or so on) in array1 it would be also same on array2. Also if array1 count is 5 then array2 count also 5.

var array1 = [ { "sku": "35189424", "price": 107800, "isNew": false, "name": "Product Title A" }, { "sku": "62617802", // duplicate "price": 107800, "isNew": false, "name": "Product Title D" }, { "sku": "GRP00437", "price": 107800, "isNew": false, "name": "Product Title B" }, { "sku": "62617802", // duplicate "price": 107800, "isNew": false, "name": "Product Title D" }, { "sku": "35189432", "price": 107800, "isNew": false, "name": "Product Title YZ" } ]; var array2 = [ { "sku": "35189424", "Url": "https://......", "rating": 2, "status": 0 }, { "sku": "62617802", // duplicate "Url": "https://......", "rating": 5, "status": 1 }, { "sku": "GRP00437", "Url": "https://......", "rating": 2, "status": 1 }, { "sku": "35189432", "Url": "https://......", "rating": 3, "status": 1 }, { "sku": "62617802", // duplicate "Url": "https://......", "rating": 5, "status": 1 } ]; var outputArray = [ { "sku": "35189424", "price": 107800, "isNew": false, "name": "Product Title A", "Url": "https://......", "rating": 2, "status": 0 }, { "sku": "62617802", // duplicate "price": 107800, "isNew": false, "name": "Product Title D", "Url": "https://......", "rating": 5, "status": 1 }, { "sku": "GRP00437", "price": 107800, "isNew": false, "name": "Product Title B", "Url": "https://......", "rating": 2, "status": 1 }, { "sku": "62617802", // duplicate "price": 107800, "isNew": false, "name": "Product Title D", "Url": "https://......", "rating": 5, "status": 1 }, { "sku": "35189432", "price": 107800, "isNew": false, "name": "Product Title YZ", "Url": "https://......", "rating": 3, "status": 1 } ];

3
  • var outputArray = [...array1, ...array2]; Commented Sep 25, 2020 at 16:01
  • Does this answer your question? Merge duplicate objects in array of objects Commented Sep 25, 2020 at 16:02
  • No, seems I wanted to keep a saperate obj and your eaxmaple is combind the value into one Commented Sep 25, 2020 at 16:08

5 Answers 5

3

You could take an object for grouping same sku and shift objects for keeping the same order fro merging.

const array1 = [{ sku: "35189424", price: 107800, isNew: false, name: "Product Title A" }, { sku: "62617802", price: 107800, isNew: false, name: "Product Title D" }, { sku: "GRP00437", price: 107800, isNew: false, name: "Product Title B" }, { sku: "62617802", price: 107800, isNew: false, name: "Product Title D" }, { sku: "35189432", price: 107800, isNew: false, name: "Product Title YZ" }], array2 = [{ sku: "35189424", Url: "https://......", rating: 2, status: 0 }, { sku: "62617802", Url: "https://......", rating: 5, status: 1 }, { sku: "GRP00437", Url: "https://......", rating: 2, status: 1 }, { sku: "35189432", Url: "https://......", rating: 3, status: 1 }, { sku: "62617802", Url: "https://......", rating: 5, status: 1 }], skus = array2.reduce((r, o) => ((r[o.sku] = r[o.sku] || []).push(o), r), { }), result = array1.map(o => ({ ...o, ...skus[o.sku].shift() })); console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

11 Comments

if I wanted to keep array1 is a main?
looks like your ??= getting eeor for me.
it was a logical nullish assignment ??=. please see edit.
If the array2 length is greater than array1, does this work?
array1 is my base array, and if array2 having more data greater than array1 will it work?
|
0

There are many ways to do this, here is one:

both = array1.concat(array2); const merged = {}; both.forEach(entry => { !merged[entry.sku] && (merged[entry.sku] = {}); Object.assign(merged[entry.sku], entry); }); const result = Object.values(merged); console.log(result); 

1 Comment

I have an another JS question, It would be helpfull if you could take a look there stackoverflow.com/questions/68737245/…
0

So basically you need to merge items having the same indices?

var array1 = [{"sku":"35189424","price":107800,"isNew":false,"name":"Product Title A"},{"sku":"62617802","price":107800,"isNew":false,"name":"Product Title D"},{"sku":"GRP00437","price":107800,"isNew":false,"name":"Product Title B"},{"sku":"62617802","price":107800,"isNew":false,"name":"Product Title D"},{"sku":"35189432","price":107800,"isNew":false,"name":"Product Title YZ"}]; var array2 = [{"sku":"35189424","Url":"https://......","rating":2,"status":0},{"sku":"62617802","Url":"https://......","rating":5,"status":1},{"sku":"GRP00437","Url":"https://......","rating":2,"status":1},{"sku":"35189432","Url":"https://......","rating":3,"status":1},{"sku":"62617802","Url":"https://......","rating":5,"status":1}]; console.log(array1.map((item, i) => ({...item, ...array2[i]})))

2 Comments

where you map by key (SKU)?
I have an another JS question, It would be helpfull if you could take a look there stackoverflow.com/questions/68737245/…
0

I borrowed ideas from @Nina, but with simpler constructs.

const d1 = {}; const d2 = {}; const outputArray = []; const addElement = (d, el) => { if (!d[el.sku]) d[el.sku] = [el]; else d[el.sku].push(el) }; array1.forEach(el => addElement(d1, el)); array2.forEach(el => addElement(d2, el)); for (let k in d1) { const res = d1[k].map((el, ind) => ({...el, ...d2[k][ind]})); outputArray.push(...res); } console.log(outputArray); 

Comments

0
let result = array1.reduce((acc, cv) => { var objj = array2.filter((obj) => obj.sku === cv.sku); if (!acc.some((item) => item.sku === cv.sku)) { let p = Object.assign({}, ...[cv], ...objj); acc.push(p); } return acc; }, []); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.