Sunday, May 19, 2024
135
rated 0 times [  139] [ 4]  / answers: 1 / hits: 90439  / 9 Years ago, tue, april 14, 2015, 12:00:00

For example if I have two objects:



var foo = {
x: bar,
y: baz
}


and



var oof = {}


and I wanted to transfer the x and y values from foo to oof. Is there a way to do that using the es6 destructuring syntax?



perhaps something like:



oof{x,y} = foo

More From » ecmascript-6

 Answers
34

While ugly and a bit repetitive, you can do



({x: oof.x, y: oof.y} = foo);


which will read the two values of the foo object, and write them to their respective locations on the oof object.



Personally I'd still rather read



oof.x = foo.x;
oof.y = foo.y;


or



['x', 'y'].forEach(prop => oof[prop] = foo[prop]);


though.


[#67084] Saturday, April 11, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
daijac

Total Points: 568
Total Questions: 120
Total Answers: 108

Location: Virgin Islands (U.S.)
Member since Fri, May 7, 2021
3 Years ago
;