Reputation: 419
I need to merge multiple json objects by common IDs. My issue is that my objects have different keys for the ID.
var object1 = [
{ "name":"apples" ,"w": 1, "x": 2 },
{ "name":"banana" ,"w": 1, "x": 2 },
{ "name":"cherry" ,"w": 1, "x": 2 },
];
var object2 = [
{ "type":"banana" ,"y": 3, "x": 4 },
{ "type":"cherry" ,"y": 3, "x": 4 },
];
I would like to obtain :
var object1 = [
{ "name":"apples" ,"w": 1, "x": 2 },
{ "name":"banana" ,"w": 1, "x": 4, "y": 3 },
{ "name":"cherry" ,"w": 1, "x": 4, "y": 3 },
];
I want to use the same Array [object1] instead of creating a new one. I created a codepen here
Upvotes: 3
Views: 106
Reputation: 126
I made a solution for your problem, can you try it ?
var object1 = [
{ "name":"apples" ,"w": 1, "x": 2 },
{ "name":"banana" ,"w": 1, "x": 2 },
{ "name":"cherry" ,"w": 1, "x": 2 },
];
var object2 = [
{ "type":"banana" ,"y": 3, "x": 4 },
{ "type":"cherry" ,"y": 3, "x": 4 },
];
function mergeObject(obj1,obj2){
var obj3 = {};
for (var attrname in obj1) { obj3[attrname] = obj1[attrname]; }
for (var attrname in obj2) { obj3[attrname] = obj2[attrname]; }
return obj3;
}
function mergeObjectInArray(firstArray, firstArrayKey, secondaryArray, secondaryArrayKey) {
var resultArray = new Array();
for(var firstArrayIndex in firstArray) {
var firstArrayObject = firstArray[firstArrayIndex];
var resultArrayObject = firstArrayObject;
for(var secondaryArrayIndex in secondaryArray) {
var secondaryArrayObject = secondaryArray[secondaryArrayIndex];
if(firstArrayObject[firstArrayKey] === secondaryArrayObject[secondaryArrayKey]) {
resultArrayObject = mergeObject(firstArrayObject,secondaryArrayObject);
delete resultArrayObject[secondaryArrayKey];
}
}
resultArray.push(resultArrayObject);
}
return resultArray;
}
var resultArray = mergeObjectInArray(object1, "name", object2, "type");
// Assuming JSON.stringify - not available in IE<8
console.log(JSON.stringify( resultArray ) );
Upvotes: 0
Reputation: 122037
You can use reduce()
to create new array and find to check if object with same name exists in object2
object with same type.
var object1 = [
{ "name":"apples" ,"w": 1, "x": 2 },
{ "name":"banana" ,"w": 1, "x": 2 },
{ "name":"cherry" ,"w": 1, "x": 2 },
];
var object2 = [
{ "type":"banana" ,"y": 3, "x": 4 },
{ "type":"cherry" ,"y": 3, "x": 4 },
];
var result = object1.reduce(function(r, e) {
var o = object2.find(a => e.name == a.type);
r.push(o ? Object.assign({}, e, {x: o.x, y: o.y}) : e);
return r;
}, [])
console.log(result)
Upvotes: 2
Reputation: 42352
Loop through object2
and update the fruits if found using Array.prototype.find
- see demo below:
var object1 = [{ "name":"apples" ,"w": 1, "x": 2 },{ "name":"banana" ,"w": 1, "x": 2 },{ "name":"cherry" ,"w": 1, "x": 2 }];
var object2 = [{ "type":"banana" ,"y": 3, "x": 4 },{"type":"cherry" ,"y": 3, "x": 4 }];
object2.forEach(function(e){
var found = object1.find(function(k){
return k.name === e.type;
});
if(found) {
found.x = e.x;
found.y = e.y;
}
});
console.log(object1);
.as-console-wrapper{top:0;max-height:100%!important;}
Upvotes: 2