0

I want to merge two arrays both array contains another array inside. refer below two arrays.

const arr1=
[{"projectId":30278,"projectName":null,"details":[{"amount":"9097457.11","currency":"USD","paymentDate":"2016-05-16T00:00:00"}]},{"projectId":37602,"projectName":null,"details":[{"amount":"8234743.0","currency":"USD","paymentDate":"2019-04-30T00:00:00"},{"amount":"8234743.0","currency":"USD","paymentDate":"2019-04-23T00:00:00"}]}]

const arr2= 
[{"projectId":30278,"projectName":null,"details":[{"amount":"8097457.11","currency":"USD","paymentDate":"2016-05-16T00:00:00"}]},{"projectId":37602,"projectName":null,"details":[{"amount":"7234743.0","currency":"USD","paymentDate":"2019-04-30T00:00:00"},{"amount":"7234743.0","currency":"USD","paymentDate":"2019-04-23T00:00:00"}]}]

when i used ES6 spread operator, both values are appended to single array. But I want to merge based upon prjectId in that array.

So after merge, i need to get the result like below

const result =
 [{"projectId":30278,"projectName":null,"details":[{"amount":"9097457.11","currency":"USD","paymentDate":"2016-05-16T00:00:00"},
{"amount":"8097457.11","currency":"USD","paymentDate":"2016-05-16T00:00:00"}
]},
{"projectId":37602,"projectName":null,"details":[{"amount":"8234743.0","currency":"USD","paymentDate":"2019-04-30T00:00:00"},{"amount":"8234743.0","currency":"USD","paymentDate":"2019-04-23T00:00:00"},
{"amount":"7234743.0","currency":"USD","paymentDate":"2019-04-30T00:00:00"},{"amount":"7234743.0","currency":"USD","paymentDate":"2019-04-23T00:00:00"}
]}]
6
  • Your arr1 and arr2 have syntax errors Commented May 16, 2019 at 11:25
  • @krishna what is the error Commented May 16, 2019 at 11:28
  • I don't see your array getting closed. Commented May 16, 2019 at 11:30
  • edited the array. now both are valid arrays only. Commented May 16, 2019 at 11:30
  • Possible duplicate of How to merge two arrays in JavaScript and de-duplicate items Commented May 16, 2019 at 11:34

3 Answers 3

1

const arr1=
[{"projectId":30278,"projectName":null,"details":[{"amount":"9097457.11","currency":"USD","paymentDate":"2016-05-16T00:00:00"}]},{"projectId":37602,"projectName":null,"details":[{"amount":"8234743.0","currency":"USD","paymentDate":"2019-04-30T00:00:00"},{"amount":"8234743.0","currency":"USD","paymentDate":"2019-04-23T00:00:00"}]}]

const arr2= 
[{"projectId":30278,"projectName":null,"details":[{"amount":"8097457.11","currency":"USD","paymentDate":"2016-05-16T00:00:00"}]},{"projectId":37602,"projectName":null,"details":[{"amount":"7234743.0","currency":"USD","paymentDate":"2019-04-30T00:00:00"},{"amount":"7234743.0","currency":"USD","paymentDate":"2019-04-23T00:00:00"}]}]

var fullArray = [...arr1,...arr2];
var mergedData ={};
fullArray.forEach(function(data){
  if(mergedData[data.projectId]){
    mergedData[data.projectId]["details"] = mergedData[data.projectId]["details"].concat(data.details)
  } else {
  mergedData[data.projectId] = data;
  }
})
console.log(Object.values(mergedData))

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

Comments

0

You can easily achieve that using Lodash unionBy function.

const result = _.unionBy(arr1, arr2, 'projectId')

2 Comments

can we achieve through plain javascript ? instead of custom script
Lodash is an open source library, so feel free to copy their code if you don't want to use it directly. github.com/lodash/lodash/blob/master/unionBy.js
0

You can also try this in this case scenario.

let mergedArr = [];    
let projectIdsArr1 = arr1.map(item => item.projectId);    
arr2.map(outerLoopItem => { 
    if (projectIdsArr1.includes(outerLoopItem.projectId)) {
      let found = arr1.find(innerLoopItem => innerLoopItem.projectId === outerLoopItem.projectId);
      found.details = [...found.details, ...outerLoopItem.details];
      mergedArr.push(found);
    } else mergedArr.push(outerLoopItem);
});
console.log(mergedArr);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.