使用 Underscore.js 基于 1 个字段合并 2 个对象 [英] Merge 2 objects based on 1 field using Underscore.js
问题描述
我有 2 个对象数组:
var a = [{ "image_id": 631293, "score": 73 },{ "image_id": 11848407, "score": 56 }];变量 b = [{image_id":631293,article_id":173},{image_id":11848407,article_id":121 }];
我尝试应用如何将对象数组与下划线js合并a> 但不幸的是它根本不起作用.
由于 image_id
,我需要合并两个对象数组.
所以我尝试了这个:
_.values(_.extendOwn(_.indexBy(a, 'image_id'),_.indexBy(b, 'image_id')))
但输出只是返回给我这个对象数组 a
我需要:
<预><代码>[{"image_id": 631293, "score": 73, "article_id": 173},{"image_id": 11848407, "score": 56, "article_id": 121}]如何在不循环检查数组的每个元素并使用 findWhere
的情况下实现这一点?
使用 indexBy
是一个好的开始.然后循环并扩展一个:
var indexed = _.indexBy(a, 'image_id');_.each(b, function(obj) {var master = indexed[obj.image_id];if (master) _.extend(master, obj);});
或者如果你想要一个新数组并让原始对象保持不变:
var 结果 = _.map(b, function(obj) {var master = indexed[obj.image_id];返回 _.extend({}, master, obj);});
var a = [{image_id":11848407,分数":56}, {image_id":631293,分数":73}, {"image_id": "虚拟",分数":1}];var b = [{"image_id": "测试",文章编号":0}, {image_id":631293,文章编号":173}, {image_id":11848407,文章编号":121}];var indexed = _.indexBy(a, 'image_id');_.each(b, function(obj) {var master = indexed[obj.image_id];if (master) _.extend(master, obj);});console.log(a);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
I have 2 arrays of objects:
var a = [
{ "image_id": 631293, "score": 73 },
{ "image_id": 11848407, "score": 56 }
];
var b = [
{ "image_id": 631293, "article_id": 173 },
{ "image_id": 11848407, "article_id": 121 }
];
I have tried to apply method described in How can i merge array of objects with underscore js but unfortunately it does not work at all.
My need is to merge both arrays of objects thanks to image_id
.
So I tried this :
_.values(
_.extendOwn(
_.indexBy(a, 'image_id'),
_.indexBy(b, 'image_id')
)
)
but output just return me this array of object a
I need to get :
[
{"image_id": 631293, "score": 73, "article_id": 173},
{"image_id": 11848407, "score": 56, "article_id": 121}
]
How can I achieve this without doing a loop to check every element of array and use a findWhere
?
Using indexBy
is a good start. Then loop and extend one:
var indexed = _.indexBy(a, 'image_id');
_.each(b, function(obj) {
var master = indexed[obj.image_id];
if (master) _.extend(master, obj);
});
or if you want a new array and let the original objects untouched:
var result = _.map(b, function(obj) {
var master = indexed[obj.image_id];
return _.extend({}, master, obj);
});
var a = [{
"image_id": 11848407,
"score": 56
}, {
"image_id": 631293,
"score": 73
}, {
"image_id": "dummy",
"score": 1
}];
var b = [{
"image_id": "test",
"article_id": 0
}, {
"image_id": 631293,
"article_id": 173
}, {
"image_id": 11848407,
"article_id": 121
}];
var indexed = _.indexBy(a, 'image_id');
_.each(b, function(obj) {
var master = indexed[obj.image_id];
if (master) _.extend(master, obj);
});
console.log(a);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
这篇关于使用 Underscore.js 基于 1 个字段合并 2 个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!