3

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

2
  • 2
    what if a second object2 would contain uncommon fruit like { "type":"lime" ,"y": 2, "x": 4 } ? What should be the result in such case? Commented Dec 30, 2016 at 14:48
  • Possible duplicate of Merge 2 arrays of objects Commented Dec 30, 2016 at 15:34

3 Answers 3

2

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)

Sign up to request clarification or add additional context in comments.

1 Comment

Hey, your solution worked awesome, the issue I have now it that ie doesn't take .find Do you know how to get over that ?
2

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;}

Comments

0

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 ) );

 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.