1

enter image description here

Consider two JSON objects, I want to merge these two objects and delete duplicated properties

1
  • 2
    can you post the code snippet instead of text? Commented Mar 3, 2016 at 12:20

2 Answers 2

2

The Angular way to do this is by using Angular.extend

var merged_object = angular.extend({}, $scope.test1, $scope.test2)
console.log(merged_object)
Sign up to request clarification or add additional context in comments.

Comments

1

angular.forEach() will solve you problem

var obj1 = {a:"hello",b:"user1",c:"user2"},obj2={a:"hello",b:"user1"};

angular.forEach(obj1,function(value,key){
 angular.forEach(obj2,function(value2,key2){
   if(key2 === key){
    delete obj[key];
   }
 })
});

1 Comment

There's no need to loop through both objects. You can use the hasOwnProperty method to check if obj2 contains the property from obj1

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.