Destructing the destination of an array stored in an object

Let's say I have an object:

{ foo: 1, bar: 'a', baz: [1,2,3] }

How to assign a value bazto get the head and tail of an array?

I mean, the effect is the same as in the code below, except that I do not want to use an additional variable baz:

{ baz } = obj;
[head, ...tail] = baz;

I know what I can say [head, ...tail] = obj.baz, but my question is about the syntax.

+4
source share
1 answer

You can, for example, do

let {baz: [head, ...tail]} = obj;

combining templates together.

+5
source

Source: https://habr.com/ru/post/1624037/


All Articles