Reputation: 234
I have two objects
$scope.a= {"Sedan":{"Audi":["A3","A4"]},"Hatchback":{"Maruthi":["Swift"]}};
var b= {"Hatchback":{"Volkswagen":["Polo"],"Fiat":["Punto1"]}};
I need the following result after merging
{"Sedan":{"Audi":["A3","A4"]},"Hatchback":{"Maruthi":["Swift"],"Volkswagen":["Polo"],"Fiat":["Punto1"]}};
Upvotes: 1
Views: 129
Reputation: 4645
Try below snippet
$scope.extend(obj, src) {
for (var key in src) {
if (src.hasOwnProperty(key)) obj[key] = src[key];
}
return obj;
}
To merge the objects
$scope.extend($scope.a, b);
Or you can use direct jQuery function. Please make sure you should have loaded the jquery min prior to your angular min js. After loading jquery min on the DOM, please call below jQuery function
var newObj = $.extend(true, $scope.a, b);
Upvotes: 0
Reputation: 292
You can do like this..
$scope.a= {"Sedan":{"Audi":["A3","A4"]},"Hatchback":{"Maruthi":["Swift"]}};
var b= {"Hatchback":{"Volkswagen":["Polo"],"Fiat":["Punto1"]}};
var mergedobj= angular.merge($scope.a, b); //merge object b into $scope.a
see working plunkr here http://plnkr.co/edit/VpHwERUmLqx6MlwQj6JB?p=preview
Upvotes: 3
Reputation: 1
If you need a new object as a result try this:
var c = {};
angular.extend(c, $scope.a, b);
or you can do this:
angular.extend($scope.a, b);
$scope.a
will contain the merged result.
Upvotes: -1