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
While ugly and a bit repetitive, you can do
which will read the two values of the
foo
object, and write them to their respective locations on theoof
object.Personally I'd still rather read
or
though.
BabelJS plugin
If you are using BabelJS you can now activate my plugin
babel-plugin-transform-object-from-destructuring
(see npm package for installation and usage).I had the same issue described in this thread and for me it was very exhausting when you create an object from a destructuring expression, especially when you have to rename, add or remove a property. With this plugin maintaining such scenarios gets much more easier for you.
Object example
can be written as:
Array example
can be written as: