我有两个对象数组:
数组1:
var myArr1 = [];
myArr1["1"]={any:1,some:1};
myArr1["2"]={any:2,some:2};
myArr1["3"]={any:3,some:3};
ARRAY2:
var myArr2 = [];
myArr2["1"]={other:1};
myArr2["2"]={other:2};
我希望将它们的键合并到一个新的属性中,结果将是:
[
{any:1,some:1,myNewAttribute:{other:1}},
{any:2,some:2,myNewAttribute:{other:2}},
{any:3,some:3,myNewAttribute:{other:3}}
]
我试图用lodash _.merge()
来实现它,但我失败了。 _.merge
仅在第一个数组之后添加第二个数组,但与其键/ ID不匹配。
答案 0 :(得分:3)
您可以将第二个数组映射到新属性,然后再合并。
使用lodash
var data1 = [{ any: 1, some: 1 }, { any: 2, some: 2 }, { any: 3, some: 3 }],
data2 = [{ other: 1 }, { other: 2 }, { other: 3 }];
console.log(_.merge(data1, _.map(data2, x => ({ myNewAttribute: x }))));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>
使用ES6,不使用lodash
var data1 = [{ any: 1, some: 1 }, { any: 2, some: 2 }, { any: 3, some: 3 }],
data2 = [{ other: 1 }, { other: 2 }, { other: 3 }];
console.log(data1.map((a, i) => Object.assign({}, a, { myNewAttribute: data2[i] })));
答案 1 :(得分:1)
你不需要lodash:
myArr1.map((e1, idx) => Object.assign({}, e1, {myNewAttribute: myArr2[idx]}))
你可以想象并编写一个名为map2
的小函数,该函数接受两个数组,并使用这两个元素调用回调:
function map2(a1, a2, fn) {
return a1.map((elt, idx) => fn(elt, a2[idx]);
}
现在您可以将解决方案编写为
map2(myArr1, myArr2, (e1, e2) => Object.assign({}, e1, {myNewAttribute: e2}))
从程序设计的角度来看,我们在这里做的是分离问题&#34;。第一个问题是并行循环两个数组并使用每对元素执行某事的抽象操作。这就是map2
所代表的内容。第二个问题是您想要组合元素的具体方式。这就是我们传递给map2
的函数所代表的内容。通过单独编写,可以使其更加清晰,有些自我记录:
function combineObjects(e1, e2) {
return Object.assign({}, e1, {myNewAttribute: e2});
}
map2(myArr1, myArr2, combineObjects);
当然,在现实世界中,您希望处理两个数组长度不同的情况,将索引传递给回调作为第三个参数,必要时使用,支持第三个{{ 1}} - 类型参数类似thisArg
等
答案 2 :(得分:0)
你可以这样做:
var first = [{any:1,some:1},{any:2,some:2},{any:3,some:3}];
var second = [{other:1},{other:2},{other:3}];
for(var i = 0; i < first.length; i++){
if(first[i] && second[i]){
first[i]['mycustomitem'] = second[i];
}
}
console.log(first);
答案 3 :(得分:0)
为了证明,我在30分钟前做了什么评论 -
How to merge two dictionaries in javascript -
可能有</b>
方法......
...首先以 lodash 为例提供......
reduce
&#13;
var
myArr1 = [
{any: 1, some: 1},
{any: 2, some: 2},
{any: 3, some: 3}
],
myArr2 = [
{other: 1},
{other: 2}
],
mergedObjectList = _.reduce(myArr1, function (collector, item_1, idx) {
var
item_2 = collector[idx],
merger = _.assign({}, item_1, item_2);
// or whatever one wants to do to `merger` with `myNewAttribute`
collector[idx] = merger;
return collector;
}, _.clone(myArr2));
console.log("myArr1 : ", myArr1);
console.log("myArr2 : ", myArr2);
console.log("mergedObjectList : ", mergedObjectList);
&#13;
...其次仅作为语言核心基于的示例......
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>
&#13;
答案 4 :(得分:-1)
尝试此功能:
function mergeDictionary(_dctn1,_dctn2)
{
var newDict = [];
for(var i in _dctn1)
{
newDict[i] = _dctn1[i];
}
for(var j in _dctn2)
{
if(newDict[j] == undefined)
{
newDict[j] = _dctn2[j];
}
else
{
for(var k in _dctn2[j])
{
newDict[j][k] = _dctn2[j][k];
}
}
}
return newDict;
}
var myArr1 = [];
myArr1["1"]={any:1,some:1};
myArr1["2"]={any:2,some:2};
myArr1["3"]={any:3,some:3};
var myArr2 = [];
myArr2["1"]={other:1};
myArr2["2"]={other:2};
console.log(mergeDictionary(myArr1, myArr2));