我正在尝试创建一个从两个独立对象获取信息的对象(取自ajax调用)。基本上我们有一个标签列表,还有一个元素列表,这些项是相关的,但是标签对象不包含我填充所有数据所需的所有信息。 例如:
标签列表:
[
{"id": 1, "name": "yadda", "description": "yadda yadda"},
{"id": 2, "name": "yadda1", "description": "yadda yadda1"},
{"id": 7, "name": "yadda2", "description": "yadda yadda2"},
{"id": 10, "name": "yadda3", "description": "yadda yadda3"}
]
元素列表(更多信息):
[
{"id": 1, "icon": "icon1.gif"},
{"id": 2, "icon": "icon2.gif"},
{"id": 7, "icon": "icon3.gif"},
{"id": 10, "icon": "icon4.gif"}
]
我需要通过ID比较两个对象,并将它们组合成一个新对象,使我能够访问这两个对象中的所有数据。
如果它有帮助,这是一个有角度的项目,我已经在使用下划线了,我相信必须有一些方法用下划线来做到这一点,但我不是很熟悉它。
答案 0 :(得分:1)
又快又脏:
var a = [
{"id": 1, "name": "yadda", "description": "yadda yadda"},
{"id": 2, "name": "yadda1", "description": "yadda yadda1"},
{"id": 7, "name": "yadda2", "description": "yadda yadda2"},
{"id": 10, "name": "yadda3", "description": "yadda yadda3"}
],
b = [
{"id": 1, "icon": "icon1.gif"},
{"id": 2, "icon": "icon2.gif"},
{"id": 7, "icon": "icon3.gif"},
{"id": 10, "icon": "icon4.gif"}
];
var result = a.map(function(v){
var ret;
$.each(b, function(k, v2){
if(v2.id === v.id){
ret = $.extend({}, v2, v); // merge the objects in to a new one
return false; // break the loop
}
});
return ret;
});
console.log(result);
如您所见,这假设两个数组中的对象之间存在1:1的关系。
答案 1 :(得分:0)
对于它的价值,没有图书馆,只有香草js
完成工作的功能:
function mergeObjArrays(list1, list2) {
var mergedList = [],
i = 0,
j = 0,
k = 0,
l1 = list1.length,
l2 = list2.length,
listMatchFound = false,
mergedObj,
mergeMatchFound = false;
for (i = 0; i < l1; i++) {
for (j = 0; j < l2; j++) {
if (list1[i].id === list2[j].id) {
listMatchFound = true;
mergedObj = mergeObj(list1[i], list2[j]);
for (k = 0; k < mergedList.length; k++) {
if (mergedList[k].id === mergedObj.id) {
mergedObj = mergeObj(mergedList[k], mergedObj);
mergedList[k] = mergedObj;
mergeMatchFound = true;
break;
}
}
if (!mergeMatchFound) {
mergedList.push(mergedObj);
} else {
mergeMatchFound = false; //reset ready for another iteration
}
}
}
if (!listMatchFound) {
mergedList.push(list1[i]);
} else {
listMatchFound = false; //reset ready for another iteration
}
}
return mergedList;
function mergeObj(obj1, obj2) {
for (var o in obj1) {
obj2[o] = obj1[o];
}
return obj2;
}
}
使用示例
var a = [
{"id": 1, "name": "yadda", "description": "yadda yadda"},
{"id": 2, "name": "yadda1", "description": "yadda yadda1"},
{"id": 7, "name": "yadda2", "description": "yadda yadda2"},
{"id": 10, "name": "yadda3", "description": "yadda yadda3"}
];
var b = [
{"id": 7, "icon": "icon3.gif"},
{"id": 10, "icon": "icon4.gif"},
{"id": 2, "icon": "icon2.gif"},
{"id": 2, "title": "Number 2 title"},
{"id": 1, "icon": "icon1.gif"}
];
//used like
var result = mergeObjArrays(a,b);
console.log(result)
//outputs (although not necessarily in order)
[
{"id": 1, "name": "yadda", "description": "yadda yadda","icon": "icon1.gif"},
{"id": 2, "name": "yadda1", "description": "yadda yadda1","icon": "icon2.gif","title": "Number 2 title"},
{"id": 7, "name": "yadda2", "description": "yadda yadda2","icon": "icon3.gif"},
{"id": 10, "name": "yadda3", "description": "yadda yadda3","icon": "icon4.gif"}
]
这将影响基于set1中的对象(在此示例中为a)进行合并,(即如果在set1中发生则包括对象,如果在set1和set2中则包含合并对象,但如果仅在set2中发生则不包括),以及处理1对多关系(即set2中的对象的多个实例将与set1的obj合并为一个对象)并且对象的顺序无关紧要。你甚至可以更进一步传入两个对象中的关键字(公共元素)字段,在这个例子中,“id”被硬连线到mergeObjArrays函数。
答案 2 :(得分:0)
http://plnkr.co/edit/77kMDeqkGvyVK87FISGl?p=info
function combine(x,y){
var z = x;
for (i=0; i<x.length; i++){
for (j=0; j<y.length; j++){
if (x[i].id === y[j].id) {
z[i].icon = y[j].icon;
break;
}
}
}
return z;
}
答案 3 :(得分:0)
似乎从SQL JOIN迁移到Javascript代码。
这是LEFT JOIN工具:
var hash2 = _.object(
_.map(list2,function(row){
return [row.id,_.omit(row,"id")];
})
);
var newList = _.map(list1,function(row){
return _.extend(row,hash2[row.id]||{});
});