我有两个数组,其中包含一个公共字段成员。我怎样才能轻松合并主题?
例如:
var arr1 = [{
"member" : ObjectId("57989cbe54cf5d2ce83ff9d6"),
"bank" : ObjectId("575b052ca6f66a5732749ecc"),
"country" : ObjectId("575b0523a6f66a5732749ecb")
},
{
"member" : ObjectId("57989cbe54cf5d2ce83ff9d8"),
"bank" : ObjectId("575b052ca6f66a5732749ecc"),
"country" : ObjectId("575b0523a6f66a5732749ecb")
}];
var arr2 = [{
"member" : ObjectId("57989cbe54cf5d2ce83ff9d6"),
"name" : 'xxxxxx',
"age" : 25
},
{
"member" : ObjectId("57989cbe54cf5d2ce83ff9d8"),
"name" : 'yyyyyyyyyy',
"age" : 26
}];
预期:
var merge = [{
"member" : ObjectId("57989cbe54cf5d2ce83ff9d6"),
"bank" : ObjectId("575b052ca6f66a5732749ecc"),
"country" : ObjectId("575b0523a6f66a5732749ecb"),
"name" : 'xxxxxx',
"age" : 25
},
{
"member" : ObjectId("57989cbe54cf5d2ce83ff9d8"),
"bank" : ObjectId("575b052ca6f66a5732749ecc"),
"country" : ObjectId("575b0523a6f66a5732749ecb"),
"name" : 'yyyyyyyyyy',
"age" : 26
}];
我试过
var merge = _.unionBy(arr1, arr2, 'member');
但未按预期合并。显示 array1 值。任何人都可以帮助我吗?
答案 0 :(得分:41)
如果两个阵列的顺序正确;其中每个项目对应于其关联的成员标识符,您只需使用。
var merge = _.merge(arr1, arr2);
以下是简短版本:
var merge = _.chain(arr1).zip(arr2).map(function(item) {
return _.merge.apply(null, item);
}).value();
或者,如果数组中的数据没有任何特定顺序,您可以按成员值查找关联项目。
var merge = _.map(arr1, function(item) {
return _.merge(item, _.find(arr2, { 'member' : item.member }));
});
您可以轻松将其转换为mixin。请参阅以下示例:
_.mixin({
'mergeByKey' : function(arr1, arr2, key) {
var criteria = {};
criteria[key] = null;
return _.map(arr1, function(item) {
criteria[key] = item[key];
return _.merge(item, _.find(arr2, criteria));
});
}
});
var arr1 = [{
"member": 'ObjectId("57989cbe54cf5d2ce83ff9d6")',
"bank": 'ObjectId("575b052ca6f66a5732749ecc")',
"country": 'ObjectId("575b0523a6f66a5732749ecb")'
}, {
"member": 'ObjectId("57989cbe54cf5d2ce83ff9d8")',
"bank": 'ObjectId("575b052ca6f66a5732749ecc")',
"country": 'ObjectId("575b0523a6f66a5732749ecb")'
}];
var arr2 = [{
"member": 'ObjectId("57989cbe54cf5d2ce83ff9d8")',
"name": 'yyyyyyyyyy',
"age": 26
}, {
"member": 'ObjectId("57989cbe54cf5d2ce83ff9d6")',
"name": 'xxxxxx',
"age": 25
}];
var arr3 = _.mergeByKey(arr1, arr2, 'member');
document.body.innerHTML = JSON.stringify(arr3, null, 4);

body { font-family: monospace; white-space: pre; }

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.14.0/lodash.min.js"></script>
&#13;
答案 1 :(得分:23)
使用_.keyBy()
,merge词典为两个数组创建词典,并将结果转换为_.values()
的数组。这样,数组的顺序并不重要。此外,它还可以处理不同长度的数组。
const ObjectId = (id) => id; // mock of ObjectId
const arr1 = [{"member" : ObjectId("57989cbe54cf5d2ce83ff9d8"),"bank" : ObjectId("575b052ca6f66a5732749ecc"),"country" : ObjectId("575b0523a6f66a5732749ecb")},{"member" : ObjectId("57989cbe54cf5d2ce83ff9d6"),"bank" : ObjectId("575b052ca6f66a5732749ecc"),"country" : ObjectId("575b0523a6f66a5732749ecb")}];
const arr2 = [{"member" : ObjectId("57989cbe54cf5d2ce83ff9d6"),"name" : 'xxxxxx',"age" : 25},{"member" : ObjectId("57989cbe54cf5d2ce83ff9d8"),"name" : 'yyyyyyyyyy',"age" : 26}];
const merged = _(arr1) // start sequence
.keyBy('member') // create a dictionary of the 1st array
.merge(_.keyBy(arr2, 'member')) // create a dictionary of the 2nd array, and merge it to the 1st
.values() // turn the combined dictionary to array
.value(); // get the value (array) out of the sequence
console.log(merged);
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.14.0/lodash.min.js"></script>
&#13;
使用ES6 Map
将数组Concat,并将reduce组合数组传递给Map。使用Object#assign将具有相同member
的对象组合到新对象,并存储在地图中。将地图转换为包含Map#values和spread的数组:
const ObjectId = (id) => id; // mock of ObjectId
const arr1 = [{"member" : ObjectId("57989cbe54cf5d2ce83ff9d8"),"bank" : ObjectId("575b052ca6f66a5732749ecc"),"country" : ObjectId("575b0523a6f66a5732749ecb")},{"member" : ObjectId("57989cbe54cf5d2ce83ff9d6"),"bank" : ObjectId("575b052ca6f66a5732749ecc"),"country" : ObjectId("575b0523a6f66a5732749ecb")}];
const arr2 = [{"member" : ObjectId("57989cbe54cf5d2ce83ff9d6"),"name" : 'xxxxxx',"age" : 25},{"member" : ObjectId("57989cbe54cf5d2ce83ff9d8"),"name" : 'yyyyyyyyyy',"age" : 26}];
const merged = [...arr1.concat(arr2).reduce((m, o) =>
m.set(o.member, Object.assign(m.get(o.member) || {}, o))
, new Map()).values()];
console.log(merged);
&#13;