为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

茅侃侃 2019-04-18 18:15:54
我想合并两个数组,两个数组都包含另一个数组。请参阅以下两个数组。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"}]}]
查看完整描述

3 回答

?
幕布斯7119047

TA贡献1794条经验 获得超8个赞

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


查看完整回答
反对 回复 2019-05-17
?
尚方宝剑之说

TA贡献1788条经验 获得超4个赞

您也可以在这种情况下尝试此操作。


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


查看完整回答
反对 回复 2019-05-17
?
慕码人8056858

TA贡献1803条经验 获得超6个赞

您可以使用Lodash unionBy函数轻松实现该功能

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


查看完整回答
反对 回复 2019-05-17
  • 3 回答
  • 0 关注
  • 2389 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信