Is it possible to destructure an object into existing variables?

Is there any way to do this without renaming the destructuring variables?. Some like this and updating point avoiding const definition?

const point = = complexPoint 
The expected result should be as using object destructuring
const x=1, y=2 // Those should be 1 and 2 const point =
Juan Caicedo asked Dec 19, 2019 at 20:00 Juan Caicedo Juan Caicedo 1,523 18 18 silver badges 33 33 bronze badges

Not sure what you want to achieve there. Are you trying to extract with destructure and then create object?

Commented Dec 19, 2019 at 20:07 Yes, I am trying to extract just those variables Commented Dec 19, 2019 at 20:08 What's the content of complex point? Commented Dec 19, 2019 at 20:15 it is a big complex object like Commented Dec 19, 2019 at 20:20

4 Answers 4

You can do this with array destructuring, i.e.:

const complexPoint = [1,2]; let x, y; [x,y] = complexPoint; 

As for object destructuring, an equivalent syntax would not work as it would throw off the interpreter:

const complexPoint = ; let x, y; = complexPoint; // THIS WOULD NOT WORK 

A workaround could be:

const complexPoint = ; let x, y; [x,y] = [complexPoint.x, complexPoint.y]; // Or [x,y] = Object.values(complexPoint); 

UPDATE:

It appears you can destructure an object into existing variables by wrapping the assignment in parenthesis and turning it into an expression. So this should work:

const complexPoint = ; let x, y; ( = complexPoint); // THIS WILL WORK