I was doing some destructuring in ES6 and ran into an unexpected situation. Here's a trivialized version of what happened.
let obj = {x: {y: 5}};
let {x: {y}} = obj;
console.log(x); // x is not defined
console.log(y); // 5
In my use case, I needed access to both x
and y
. I would have expected x
to have been destructured as well. Instead, to get the desired effect, I had to do this:
let obj = {x: {y: 5}};
let {x, x: {y}} = obj;
console.log(x); // {"y":5}
console.log(y); // 5
However, I think {x, x: {y}}
looks weird and unintuitive. Is there a destructuring secret I'm not aware of, or is this just a tiny destructuring pitfall?