ES6根据关键的Typescript或Javascript合并两个数组(数组包含另一个数组)

我想合并两个数组,两个数组都包含另一个数组。请参阅以下两个数组。


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"}]}]

当我使用ES6扩展运算符时,两个值都附加到单个数组。但我想基于该数组中的prjectId进行合并。


所以在合并之后,我需要得到如下的结果


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"}

]}]


茅侃侃
浏览 2393回答 3
3回答

幕布斯7119047

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))

尚方宝剑之说

您也可以在这种情况下尝试此操作。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);

慕码人8056858

您可以使用Lodash unionBy函数轻松实现该功能。const result = _.unionBy(arr1, arr2, 'projectId')
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript