我有一个问题是将两个JSON对象(每个来自另一个数组)合并到一个新的JSON对象中,在新的数组中(合并必须在给定条件下发生)。
示例:
First Array:
var array1 = [{box:123,name:xxx,amount:xxx},{box:321,....},...]
var array2 = [{_id:123,look:xxx,title:myBox1},{_id:321,.....},...]
var newArray = [{box:123,name:xxx,amount:xxx,look:xxx,title:myBox1},...]
我基本上需要什么:
迭代Array1,遍历Array2。如果框(array1) == _id (array2)
将两个对象的所有属性合并为一个新的JSON,并将其放入新数组3的第一个空闲槽中。
我希望这个例子有助于了解我想要做的事情。什么是最好的解决方案?
通常情况下,两个数组中的第一个属性也是需要进行比较的属性,但是如果解决方案在我想要进行比较的情况下也可以进行比较,那么它就可以了。第一个数组中的第一个属性,第二个数组中的第三个属性。
希望有人可以帮助我!
答案 0 :(得分:1)
您可以通过两个循环实现此目的。第一个迭代存储在array1
中的对象,第二个迭代array2
中匹配对象的属性。试试这个:
var newArray = [];
for (var i = 0; i < array1.length; i++) {
var obj = array1[i];
if (array2[i] && obj.box == array2[i]._id) {
for (key in array2[i]) {
obj[key] = array2[i][key];
}
newArray.push(obj);
}
};
答案 1 :(得分:0)
你可以试试这个:
array3=[]
for(item1 in array1){
for(item2 in array2){
if(item1.box == item2._id)
array3.push(Merge_objects(item1,item2))
}
}
function Merge_objects(obj1,obj2){
var obj3 = {};
for (var attrname in obj1) { obj3[attrname] = obj1[attrname]; }
for (var attrname in obj2) { obj3[attrname] = obj2[attrname]; }
return obj3;
}
答案 2 :(得分:0)
以下功能可以做到。它比其他答案略长,但更通用(您可以指定哪些属性是ID,而不是坚持box
和_id
)并且不会修改原始数组以任何方式。它也不依赖于两个数组中的项的顺序相同(即box:123
可以在array1中的索引1中,_id:123
可以在array2中的索引3中)。
/*
a1 = the first array of objects
id1 = the name of the property of each object in a1 that is its id
a2 = the second array of objects
id2 = the name of the property of each object in a2 that is its id
*/
function combine(a1, id1, a2, id2) {
// create our new array to return
var newArray = []
// iterate over a1 (the outer loop)
for(var i = 0, l = a1.length; i < l; i++) {
var outer = a1[i]; // get the current object for this iteration
// iterate over a2 (the inner loop)
for(var j = 0, m = a2.length; j < m; j++) {
var inner = a2[j]; // get the current object for this iteration of the inner loop
if(outer[id1] == inner[id2]) { // compare the ids for the outer and inner objects
var o = {}; // create a new blank object to add to newArray
o[id1] = outer[id1]; // set its id (using the same property name as that of a1)
for(var prop in outer) { // iterate over properties
if(prop != id1) { // ignore the id property, but copy the others
o[prop] = outer[prop]
}
}
for(var prop in inner) {
if(prop != id2) {
o[prop] = inner[prop]
}
}
// add this object to newArray
newArray.push(o);
}
}
}
// return newArray
return newArray;
}
用法:
var array1 = [{box:123,name:'Name 1',amount:1},{box:321,name:'Name 2',amount:2}]
var array2 = [{_id:123,look:'Look 1',title:'My box 1'},{_id:321,look:'Look 2',title:'My box 2'}]
var newArray = combine(array1, "box", array2, "_id");
答案 3 :(得分:0)
您可以使用Array.prototype.map或Array.prototype.reduce。记住time complexity!如果对象是密钥(_id
或box
)并且数组具有相同的长度:
// TODO: implement your own comparator based on _id/box type
var first = array1.sort((a, b) => a._id - b._id);
var second = array2.sort((a, b) => a.box - b.box);
var merged = sorted_first.map((item, index) => Object.assign(item, second[index]));
您可以轻松地将其调整为不同长度的情况,但您确实需要首先了解您的数据。
答案 4 :(得分:0)
Creating the dummy datas, this is what the programming should be:
var a = [{
id: 1,
name: 'name1',
}, {
id: 2,
name: 'name2',
}, {
id: 3,
name: 'name3',
}];
var b = [{
id: 1,
look: 'look1'
}, {
id: 321,
look: 'look321'
}, {
id: 3,
look: 'look3',
}
];
// put the object id : 321 and look : look321 into array a from array b
for (var i = 0; i < b.length; i++) {
var obj_b= b[i];
var id_b= b[i].id;
var idNotFound = true;
a.forEach(function(rowOfA){
var id_a = rowOfA.id;
if(id_a == id_b ){
idNotFound = false;
}
});
if( idNotFound ){
a.push( b[i] );
}//if
}
// merge the objects with same ids into array a
for (var i = 0; i < a.length; i++) {
var id_a = a[i].id;
var found = false;
for (var j = 0; j < b.length; j++) {
var id_b= b[j].id;
if(id_a == id_b){
found = b[j];
}//if
}//for2
if (found) {
a[i]["look"] = found.look;
}//if
}//for1
console.log('aa=>',a);
/*
The output is aa=> [ { id: 1, name: 'name1', look: 'look1' },
{ id: 2, name: 'name2' },
{ id: 3, name: 'name3', look: 'look3' },
{ id: 321, look: 'look321' } ]
*/