0

Is there a short syntax in ES6/7 for assigning just some selective properties of an object to another? For example I want to do this

const a = {
  x: b.x,
  y: b.y,
  z: b.z
}

it's ok with 2-3 properties but with many more it will become redundant. How can I make it simple?

Edit: this question is not about destructuring object. I know destructuring can do the job, but it's not really shorter/more handy than the example I gave above, so that's not the thing mentioned here

KwiZ
  • 1,364
  • 2
  • 15
  • 25
  • 1
    I disagree with the duplicate mark, as the other question (although very much related) specifically asks for *destructuring* while this question does not. – Stephan Bijzitter Sep 07 '15 at 15:12
  • OK, sorry I've chosen the wrong duplicate. [ES6 Structuring Assignment?](http://stackoverflow.com/q/30897961/1048572) is the proper one. Anyone with a gold badge please fix it :-) – Bergi Sep 07 '15 at 15:53
  • 2
    As found in other answers to other questions, one less-than-wonderful alternative is `const a = ({x, y, z} => ({x, y, z}))(b)`. See http://stackoverflow.com/questions/25553910/one-liner-to-take-some-properties-from-object-in-es6/25554551#25554551. –  Sep 07 '15 at 15:53
  • @torazaburo: That might be an even better duplicate. Mind to open and re-close it? – Bergi Sep 07 '15 at 15:57
  • @Bergi Sure about that? Your second proposed dup could be closer. –  Sep 07 '15 at 16:01
  • @torazaburo: Hm, dunno. The phrasing "*assigning just some selective properties of an object to another*" suggests mine, the example with creating the literal is closer to yours. – Bergi Sep 07 '15 at 16:04
  • The new duplicate is definitely a lot better :-) – Stephan Bijzitter Sep 08 '15 at 09:15

1 Answers1

1

I posted this as answer because it does work, it is just not a wise idea to use.

const b = {x: 1, y: 2, z: 3};

with (b) {
    const a = {x, y, z};
}

console.log(a);

Strict mode actually forbids using the with statement since ECMA5, so a client running and obeying strict mode will definitely hate your code if you do this.

The with statement also has other drawbacks, you can read more about those here: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/with

Stephan Bijzitter
  • 4,425
  • 5
  • 24
  • 44
  • 1
    Thanks, seems that my case has no real solution. Btw I think your last warning (I posted this as answer because it does work, it is just not a wise idea to use.) should be placed right at the start of the answer and highlighted to warn other people who may read this. – KwiZ Sep 07 '15 at 15:47
  • Actually it doesn't really work, as `const` is scoped to the `with` block in ES6. – Bergi Sep 07 '15 at 15:55
  • well that'll take just a small change to work anw :) – KwiZ Sep 07 '15 at 15:59
  • I tested this in the latest version of Chrome and it works. Since `const` is introduced in ES6, and thus requires Chrome to run it in ES6 or later, it is confirmed to work. But I do agree, the scope of the const keyword is confusing here, as I initially thought the same as you. @Bergi – Stephan Bijzitter Sep 08 '15 at 09:09
  • 1
    @StephanBijzitter: As you can see in the [ES6 compatibility table](https://kangax.github.io/compat-table/es6/), Chrome does not support block scopes yet - it just handles `const` as if it was `var`. – Bergi Sep 08 '15 at 10:07
  • Then this answer will stop working as soon as Chrome starts supporting it :-) But then the `const a` could also be changed to `a` or `var a`. – Stephan Bijzitter Sep 09 '15 at 07:17