Is there a way I can restructure part of a destructuring assignment?

173 views Asked by At

Let's say I have an array of arrays, like so:

var arrs = [
    [1, "foo", "bar", "baz"],
    [2, "bar", "baz", "qux"],
    [3, "baz", "qux", "thud"]
];

I want to use ES6's destructuring assignment to get the first element of each array as an individual variable, and repack the rest of the elements as another array. In pseudocode:

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
}

Is something like this possible?

1

There are 1 answers

8
Pointy On BEST ANSWER

That's what ... does:

for (let [first, ... rest] of arrs) {