ecmascript 6 - Is it possible to destructure an object and generate a new object in a single statement? -


const {name, slug, description, parent} = cat; const sanecat = {name, slug, description, parent}; 

in first expression, define 4 constants destructuring messy object. in second expression, combine them new object. in single step without duplicating list of interesting fields, because first expression doesn't define object, seems impossible. really? close clojurian win!

i have tried following without success. ecmascript compiler fair game, don't have option of using clojurescript.

const sanecat = {name, slug, description, parent} = cat; // name not defined const {...sanecat} = {name, slug, description} = cat;  // name not defined const sanecat = ({name, slug, description, parent} = cat); // name not defined const sanecat = ({name, slug, description, parent}) = cat; // name not defined const sanecat = {{name, slug, description, parent}} = cat; // unexpected token const sanecat = {{name, slug, description, parent}} = {cat}; // unexpected token 

unfortunately, destructuring in es6 limited assignments , parameter lists. there discussion of "pick" operator want, don't hold breath.

meanwhile, closest can come following, requires duplicating list of properties:

const sanecat =    (({name, slug, description, parent}) =>      ({name, slug, description, parent})   )(cat); 

using proposed pick syntax, able write

const sanecat = {name, slug, description, parent} # cat; 

something should show in es29. until then, can use pick utilities of sort mentioned in comment, personally, hate writing properties strings.

see one-liner take properties object in es 6.


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -