8
var { iWantThis: { andThis, andThisToo } } = x;

Is there a way to get access to all three in one destructuring call? I want to avoid two calls like so:

var { iWantThis } = x;
var { andThis, andThisToo } = iWantThis;
Kelly Selden
  • 1,238
  • 11
  • 21
  • So you're ideally trying to end up with 3 new vars: `iWantThis`, `andThis`, and `andThisToo`? Or just the second 2? – JMM Oct 15 '15 at 01:35
  • @locks answer is the most compact I can think of. – JMM Oct 15 '15 at 12:26
  • See [Unexpected destructuring behavior, having to declare a key twice when destructuring deeper](https://stackoverflow.com/q/38667095/1048572) for an explanation – Bergi Jul 16 '19 at 19:01

1 Answers1

10

The closest I can come up with is:

var { iWantThis, iWantThis: { andThis, andThisToo } } = x;

Though I'd use let instead, if I'm using ES6 ;)

locks
  • 6,537
  • 32
  • 39