ecmascript 6 - Javascript destructuring to populate existing object -


this question has answer here:

i'm using object destructuring syntax of es6. want use in order populate existing object.

i got 2 objects:

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

i want assign email object properties $scope object.

for now, i've end doing so:

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

in real life use case have more 2 properties assigned.

so, know other way improve , avoid repeating $scope in left assignation part?

you correct, can this:

object.assign($scope, email); 

however not immutable, altering $scope object (which fine in case). if want immutable operation this:

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

that return brand new object.

also, if have object rest spread feature turned on in transpiler, can this:

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

this immutable , uses object.assign behind scenes.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -