简体   繁体   English

通过键合并带下划线的数组

[英]Merge array with underscore by keys

I have 2 arrays : 我有2个数组:

[{id:1,name:"name"},{id:2,name:"name2"} ,{id:3,name:"name3"}]

[{id:1,date:"123"},{id:2,date:"456"}]

Array 1 should be updated only if the id is equal : So the array 1 will looks like 仅当id相等时才应更新数组1:因此,数组1看起来像

It should not create a new array . 它不应创建新的数组。 Only update the array 1 based on array 2 仅基于阵列2更新阵列1

[{id:1,name:"name",date:"123"},{id:2,name:"name2",date:"456"} ,{id:3,name:"name3"}]

I managed to do that with for loop on array2 and inside the for filter like the following : 我设法通过在array2上的for循环和for过滤器内执行以下操作:

._filter(array1,function(item){
 If(item.id=array2.id)
    Do smth and update the array1.date
})

How do I doing that in he best way ? 我如何以他最好的方式做到这一点? Using underscore.js 使用underscore.js

You can do it using native language like this: 您可以使用以下本地语言来做到这一点:

 const arr1 = [{id:1,name:"name"},{id:2,name:"name2"} ,{id:3,name:"name3"}]; const arr2 = [{id:1,date:"123"},{id:2,date:"456"}]; arr1.forEach((ele) => { const match = arr2.find(item => ele.id === item.id) || {}; Object.assign(ele, match); }); console.log(arr1); 

You can do something like this: 您可以执行以下操作:

Iterate over array1 and check if the id of each item exists in array2 by using the some() method. 遍历array1并使用some()方法检查每个项目的ID是否在array2中。

 var arr1 = [{id:1,name:"name"},{id:2,name:"name2"} ,{id:3,name:"name3"}]; var arr2 = [{id:1,date:"123"},{id:2,date:"456"}]; var missing = []; arr1.forEach( (item1, i) => { var isExist = arr2.some(item2 => item2.id === item1.id) if(!isExist) { missing.push(i); } }) missing.forEach(item => { arr2.push(arr1[item]); }) console.log(arr2); 

reference for some() 一些参考

Try this : 尝试这个 :

 var a = [{id:1,name:"name"},{id:2,name:"name2"} ,{id:3,name:"name3"}] ; var b = [{id:1,date:"123"},{id:2,date:"456"}] ; var i = 0, j = 0 ; while( i < a.length ) { j = 0 ; while( j < b.length) { if ( a[i].id === b[j].id ) Object.assign( a[i] , b[j] ); j++; } i++; } console.log(a) ; 

You can use forEach to iterate over the second array and use findIndex to get the matched element from first array. 您可以使用forEach遍历第二个数组,并使用findIndex从第一个数组获取匹配的元素。 If the id matches then update the object in the first array 如果id匹配,则更新第一个数组中的对象

 let arr1 = [{ id: 1, name: "name" }, { id: 2, name: "name2" }, { id: 3, name: "name3" }] let arr2 = [{ id: 1, date: "123" }, { id: 2, date: "456" }] arr2.forEach(function(acc) { let findArry1Index = arr1.findIndex(function(item) { return item.id === acc.id; }); if (findArry1Index !== -1) { arr1[findArry1Index].date = acc.date; } }); console.log(arr1) 

var a = [{id:1,name:"name"},{id:2,name:"name2"} ,{id:3,name:"name3"}];

var b = [{id:1,date:"123"},{id:2,date:"456"}];

a = _.map(a, function(e) { return _.extend(e, _.findWhere(b, {id: e.id})); });

a results in: 结果在:

0: {id: 1, name: "name", date: "123"}
1: {id: 2, name: "name2", date: "456"}
2: {id: 3, name: "name3"}

However, I guess this qualifies as "creating a new array"? 但是,我猜这符合“创建新数组”的资格吗? Maybe it can serve as an inspiration though ¯\\_(ツ)_/¯ 也许可以通过¯\\ _(ツ)_ /¯来获得启发。

You can use underscore's indexBy function to index your second array by id , and then simply use Object.assign(...) to update your first array's elements with their corresponding match by performing a lookup in the indexed elements object. 您可以使用下划线的indexBy函数通过id来索引第二个数组,然后只需使用Object.assign(...)通过在索引元素对象中执行查找来更新具有相应匹配项的第一个数组元素。

 let arr1 = [{id:1, name:"name"}, {id:2, name:"name2"}, {id:3, name:"name3"}] let arr2 = [{id:1, date:"123"}, {id:2, date:"456"}] const arr2Groups = _.indexBy(arr2, e => e.id); arr1.forEach(e => Object.assign(e, arr2Groups[e.id] || {})); console.log(arr1); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script> 

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

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