1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-05 08:08:02 +00:00

Add Destructuring The Rest Of An Array as a javascript til

This commit is contained in:
jbranchaud
2017-11-25 20:17:26 -06:00
parent 852a56260c
commit 1c02cc9605
2 changed files with 24 additions and 1 deletions

View File

@@ -0,0 +1,22 @@
# Destructuring The Rest Of An Array
ES6 offers some amount of pattern matching on arrays. This means you can do
fun stuff like grabbing a couple values and then destructuring the rest of
the array into a variable.
```javascript
> const kids = ["Mike", "Will", "Dustin", "Lucas", "Eleven", "Max"];
undefined
> const [first, second, ...rest] = kids;
undefined
> first
"Mike"
> second
"Will"
> rest
["Dustin", "Lucas", "Eleven", "Max"]
```
By using the `...` syntax with a variable name in the left-hand side of the
assignment, you are able to capture an array of whatever isn't assigned to
preceding variables.