简体   繁体   English

解析存储在object属性中的数组的赋值

[英]destructuring assignment of array stored in object property

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? 如何分配baz的值来获取数组的头部和尾部?

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

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

I know I can say [head, ...tail] = obj.baz but my question is about the syntax. 我知道我可以说[head, ...tail] = obj.baz但我的问题是关于语法。

You can for instance do 你可以这样做

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

by combining the patterns together. 通过将模式组合在一起。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM