Destroying Javascript to populate an existing object

I am using ES6 Object destructuring syntax . I want to use it to populate an existing object.

I have two objects:

let $scope = {};
let email = { from: 'cyril@so.com', to: 'you@so.com', ... };

I want to assign some properties of an object to an emailobject $scope.

I am currently doing this:

({ from: $scope.from, to: $scope.to } = email);

In my use case in real life, I have more than two properties that need to be assigned.

So, do you know another way to improve and avoid repetition $scope on the left side of the destination?

+4
source share
1 answer

You are right, you can do this:

Object.assign($scope, email);

, $scope ( ). , :

$scope = Object.assign({}, $scope, email);

.

, Object Rest Spread, :

$scope = { ...$scope, ...email };

Object.assign .

+1

Source: https://habr.com/ru/post/1627715/


All Articles