3

My requirement is compare two objects and copy updated values into first object from second object. Ex :

$scope.obj1={"id" : 1, "name" : "java"}
$scope.obj2={"id" : 1, "name" : "java4you", "gender" : "male"}

compare(destination, bj1, obj2);

Destination variable Output:

{"id" : 1, "name" : "java4you"}

The above two objects contains same keys but values are different. I have to compare obj1 and obj2 and update with matched obj2 values

4
  • You want to override and return properties with identical keys? Commented Feb 2, 2018 at 14:50
  • do you need to compare the ids before updating the "name" field? if not (the ids are always the same) you can do angular.extend(destination, obj1, obj2) which is a shallow copy. The output will be {"id" : 1, "name" : "java4you"} Commented Feb 2, 2018 at 14:56
  • 1
    Not quite @user4219031. extend will include "gender":"male". Commented Feb 2, 2018 at 14:58
  • @Mike McCaughan is correct if i use extend to copy the object properties "gender":"male" is also added to my destination object. I want to update only matched values in obj2 with obj1. I don't want additional key, value pairs which are not match with obj1. Commented Feb 3, 2018 at 11:14

5 Answers 5

1

You can create a copy of obj1 using Object.assign() in a new variable, destination and iterate through each key of obj2 using Object.keys() and array#forEach and check if key exists in destination, in case it exists, update the value in destination from the value of obj2

var obj1={"id" : 1, "name" : "java"},
    obj2={"id" : 1, "name" : "java4you", "gender" : "male"}

var updateObjectValue = (obj1, obj2) => {
  var destination = Object.assign({}, obj1);
  Object.keys(obj2).forEach(k => {
    if(k in destination) {
      destination[k] = obj2[k];
    }
  });
  return destination;
}
console.log(updateObjectValue(obj1, obj2));

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

2 Comments

Thanks for your response. Your answer is correct but in my case my json object contains huge number of elements. It is a combination of multiple inner objects and json arrays. It is not a good practice to loop such a big json object. Can you provide me any easy solution.
I would have recommended using Object.assign() but beside updating the value in object1, it will introduce all the remaining keys of object2 which you might don't want.
0

Without actually referring to methods that can be used in angularJs, You can try this:

function compare(o1, o2) {
  var biggest = o1;
  
  if (Object.keys(o2).length > Object.keys(o1).length) {
    biggest = o2;
  }

	for (let key in biggest) {
  	if (!biggest.hasOwnProperty(key)) continue;
    
    if (o1[key] != o2[key]) {
    	console.info("Difference in '"+ key +"': "+ o1[key] +" <> "+ o2[key]);
    }
	}
}

var $scope = {};

$scope.obj1 = {"id" : 1, "name" : "java"};
$scope.obj2 = {"id" : 1, "name" : "java4you", "gender" : "male"};

compare($scope.obj1, $scope.obj2);

But be careful using this, since it has many possible cases to fail.

Comments

0

Using Angularjs, you can use .equal to compare and get the result, I don't see what is not working or providing your code will only gets you the answers.

$scope.result = angular.equals($scope.obj1, $scope.obj2);
if($scope.result === true){    
$scope.obj1 = $scope.obj2;
}

Comments

0

Try this,

function compare(obj1, obj2)
{
    let obj = {};
    for(let k in obj1)
    {
         obj[k] = obj2[k];
     }
    return obj;
}

Usage should be like,

var destination = compare(obj1,obj2);

2 Comments

Thanks for the quick response. The answers not familiar to my question. My requirement is not to compare two objects. Copy the matched key, values from second obj and update that key, value in first object.
Then you can eliminate the comparison as updated answer.
0

I had to update @Bharadwaj answer for it to work for me.
for (... in ...) statements must be filtered with an if statement

    var obj1={"id" : 1, "name" : "java"},
        obj2={"id" : 1, "name" : "java4you", "gender" : "male"};

    function compare(obj1, obj2) {
      let obj = {};
      for(let k in obj1) {
         if(obj1[k] !== obj2[k]) {
           obj = Object.assign({}, obj1, obj1[k] = obj2[k]);
         }
         
      }
      return obj;
    }

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.