Consider two JSON objects, I want to merge these two objects and delete duplicated properties
2 Answers
The Angular way to do this is by using Angular.extend
var merged_object = angular.extend({}, $scope.test1, $scope.test2)
console.log(merged_object)
Comments
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
jasonscript
There's no need to loop through both objects. You can use the
hasOwnProperty method to check if obj2 contains the property from obj1