Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to destructure an object to an already defined variable? [duplicate]

People also ask

Can you Destructure array of objects?

Array DestructuringValues in arrays are destructured based on their index . The variable can be named anything you want, but the variable name associated with the index is how it is assigned. We can also assign values to variables that are already declared.

Can you Destructure nested object?

Nested Object and Array Destructuring Here's another example with an array of objects: You can destructure as deeply as you like: As you can see, keys a , b , and c are not implicitly defined, even though we pulled out nested values, firstElemOfC and remainingElementsOfC , from the array at c .


You need to use assignment without declaration syntax:

({
    screenings,
    size
} = source);

Babel REPL Example

From the linked docs:

The ( .. ) around the assignment statement is required syntax when using object literal destructuring assignment without a declaration

And obviously you need to use this as you can't redeclare a let variable. If you were using var, you could just redeclare var { screenings, size } = source;