mirror of
https://github.com/jbranchaud/til
synced 2026-01-05 16:18:01 +00:00
Add Destructuring The Rest Of An Array as a javascript til
This commit is contained in:
@@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
|
||||
For a steady stream of TILs from a variety of rocketeers, checkout
|
||||
[til.hashrocket.com](https://til.hashrocket.com/).
|
||||
|
||||
_590 TILs and counting..._
|
||||
_591 TILs and counting..._
|
||||
|
||||
---
|
||||
|
||||
@@ -218,6 +218,7 @@ _590 TILs and counting..._
|
||||
- [Create An Array Containing 1 To N](javascript/create-an-array-containing-1-to-n.md)
|
||||
- [Create Bootstrapped Apps With Yarn](javascript/create-bootstrapped-apps-with-yarn.md)
|
||||
- [Default And Named Exports From The Same Module](javascript/default-and-named-exports-from-the-same-module.md)
|
||||
- [Destructuring The Rest Of An Array](javascript/destructuring-the-rest-of-an-array.md)
|
||||
- [Enable ES7 Transforms With react-rails](javascript/enable-es7-transforms-with-react-rails.md)
|
||||
- [Expand Emojis With The Spread Operator](javascript/expand-emojis-with-the-spread-operator.md)
|
||||
- [Freeze An Object, Sorta](javascript/freeze-an-object-sorta.md)
|
||||
|
||||
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