Reputation: 15589
I have 2 angular variables defined as below:
$scope.a = {
var1: value1,
var2: value2
};
$scope.b = {
var3: value3,
var4: value4
};
I am trying to concat var1
and var2
such that there is $scope.c
as defined below:
$scope.c = {
var1: value1,
var2: value2,
var3: value3,
var4: value4
};
I am doing $scope.a = $scope.b + $scope.c
but it is not giving desired result and I am getting the value "[object Object][object Object]"
for $scope.c
.
Upvotes: 1
Views: 336
Reputation: 184
You can use the spread operator to merge two objects by
$scope.c={...$scope.a,...$scope.b};
and you can also implement using assign method.
$scope.c=Object.assign($scope.a,$scope.b);
Upvotes: 1
Reputation: 42556
You can use ES6's spread syntax when it comes to concatenating objects.
$scope.c = {...$scope.a, ...$scope.b};
Or AngularJs's angular.extend() which allows you to achieve to same result.
$scope.c = angular.extend($scope.a, $scope.b);
For instance,
const a = {
var1: 'value1',
var2: 'value2'
} ;
const b = {
var3: 'value3',
var4: 'value4'
} ;
console.log({...a, ...b});
You may check out the demo I have made here as well.
Upvotes: 4
Reputation: 86
You Can Use Object.assign For this
$scope.c = Object.assign({},$scope.a,$scope.b)
Upvotes: 3