JS ES6 Destructuring, assign entire object and destructure its properties at the same time

Viewed 701

I am trying to assign and destructure at the same time

a = {x: 1, y: 5}
let b, {x} = a

result:

b = undefined
x = 1

I want:

b = {x: 1, y: 5}
x = 1

Is there any way to achieve that ? When I try to assign with const

const c, {y} = a
      ^
Uncaught SyntaxError: Missing initializer in const declaration
1 Answers

You could wrap a in an object and destructure a with a renaming to b and take the nested x directly.

const
    a = { x: 1, y: 5 },
    { a: b, a: { x } } = { a };

console.log(b);
console.log(x);

Related