有没有办法可以重构结构分配的一部分?

Asa*_*din 3 javascript destructuring ecmascript-6

假设我有一个数组数组,如下所示:

var arrs = [
    [1, "foo", "bar", "baz"],
    [2, "bar", "baz", "qux"],
    [3, "baz", "qux", "thud"]
];
Run Code Online (Sandbox Code Playgroud)

我想使用ES6的解构赋值将每个数组的第一个元素作为单个变量,并将其余元素重新打包为另一个数组.在伪代码中:

for (let [first, *rest] of arrs) { // What is the proper way to do *rest?
    console.log(first); // Should be a number
    console.log(rest); // Should be an array of strings
}
Run Code Online (Sandbox Code Playgroud)

这样的事情可能吗?

Poi*_*nty 5

这是做什么的...:

for (let [first, ... rest] of arrs) {
Run Code Online (Sandbox Code Playgroud)

  • 注意对象示例的`// ES7`注释.如果你使用Babel它会工作,但它不在ES6中. (4认同)