mirror of
https://github.com/jbranchaud/til
synced 2026-01-04 23:58:01 +00:00
Add Destructuring The Rest Of An Array as a javascript til
This commit is contained in:
22
javascript/destructuring-the-rest-of-an-array.md
Normal file
22
javascript/destructuring-the-rest-of-an-array.md
Normal 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.
|
||||
Reference in New Issue
Block a user