4

say I have an object:

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

how to assign the value of baz to get the head and the tail of the array?

I mean so that the effect is the same as that of the code below, except for I do not want to use the extra variable baz:

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

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

1 Answer 1

5

You can for instance do

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

by combining the patterns together.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.