简体   繁体   English

使用loadash合并两个数组对象

[英]merging two array objects using loadash

Can anyone help me in merging of two array of objects please using loadash in javascript? 谁能帮助我合并两个对象数组,请在javascript中使用loadash? Below is the example arrays. 下面是示例数组。 I tried with _merge 我尝试了_merge

arr 1= [
    {
        "areaId": 1,
        "areaName": "areanam222",
        "businessExecutiveId": 1
    },
    {
        "areaId": 2,
        "areaName": "arename",
        "businessExecutiveId": 1
    }
]

arr2 =[
    {
        "id": 1,
        "name": "BN",

    }
]


arrResult =[
    {
        "areaId": 1,
        "areaName": "areanam222",
        "businessExecutiveId": 1,
        "id": 1,
        "name": "BN"    
    }, {
        "areaId": 2,
        "areaName": "arename",
        "businessExecutiveId": 1,
        "id": 1,
        "name": "BN"
    }
]

I tried with below option but it is returning only one record. 我尝试使用以下选项,但它仅返回一条记录。

var arrResult =   _(list).keyBy('businessExecutiveId').merge(_.keyBy(this.typeaheadDataList, 'id')).values() .value(); 

I also tried with below const c = _.assign([], arr1, arr2); 我也尝试了下面的const c = _.assign([],arr1,arr2);

reslut I am getting is like below 我得到的reslut如下

{ id: 1, name: "BN" }, { areaId: 1, areaName: "ASas", businessExecutiveId: 1, id: 1, name: "BN" } {id:1,名称:“ BN”},{areaId:1,areaName:“ ASas”,businessExecutiveId:1,id:1,名称:“ BN”}

Please help me 请帮我

You don't need lodash to do this. 您不需要lodash来执行此操作。 Why not just use javascripts reduce function to merge all object keys from arr2 objects into the objects of array 1, like so 为什么不使用javascript的reduce函数将arr2对象中的所有对象键合并到数组1的对象中,如下所示

 arr1.reduce((arr, obj) => { const newObj = arr2.reduce((o, insideObj) => ({ ...o, ...insideObj }), obj) return [...arr, newObj] }, []) 

If we had array comprehension , we could have used something like this: 如果我们有数组推导 ,我们可以使用如下代码:

[
   for (a of arr1) 
   for (b of arr2) 
   if(a.businessExecutiveId === b.id) {...a, ...b}
]

Without array comprehension, we use flatMap: 如果没有数组理解,我们将使用flatMap:

_(arr1).flatMap(a =>
  _(arr2).flatMap(b => 
    a.businessExecutiveId === b.id ? [{...a, ...b}] : []        
  ).value()        
).value()

 var arr1= [ { "areaId": 1, "areaName": "areanam222", "businessExecutiveId": 1 }, { "areaId": 2, "areaName": "arename", "businessExecutiveId": 1 }, { "areaId": 3, "areaName": "bname", "businessExecutiveId": 2 } ] var arr2= [ { "id": 1, "name": "BN", }, { "id": 2, "name": "CM", } ] res = _(arr1).flatMap(a => _(arr2).flatMap(b => a.businessExecutiveId === b.id ? [{...a, ...b}] : [] ).value() ).value() console.log(res) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script> 

 arr1.reduce((arr, obj) => { const newObj = arr2.reduce((o, insideObj) => ({ ...o, ...insideObj }), obj) return [...arr, newObj] }, []) 

I resolved it by using below code. 我通过使用以下代码解决了它。

_.map(list, function(item) {

    return _.merge(item, _.find(temp, function(o) {return o.id  == item.businessExecutiveId }) );
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM