Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(guide): ES6 destructuring assignment with the rest operator #35681

Merged
merged 6 commits into from
Mar 29, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Use Destructuring Assignment with the Rest Operator to Reassign Array Elements
---
## Use Destructuring Assignment with the Rest Operator to Reassign Array Elements
<!-- The article goes here, in GitHub-flavored Markdown. Feel free to add YouTube videos, images, and CodePen/JSBin embeds -->

Remember that the rest operator allows for variable numbers of arguments. In this challenge, you have to get rid of the first two elements of an array.

## Hint 1:
Expand All @@ -13,10 +13,9 @@ Assign the first two elements to two random variables.

Set the remaining part of the array to `...arr`.

=======
## Hint 1
## Hint 3:

Use destructuring to create the `arr` variable.
Use destructuring to create the `arr` variable:

```javascript
function removeFirstTwo(list) {
Expand All @@ -28,9 +27,9 @@ function removeFirstTwo(list) {
}
```

## Hint 2
## Hint 4:

Spread the `list` parameter into `arr`.
Spread the `list` parameter values into `arr`.

```javascript
function removeFirstTwo(list) {
Expand All @@ -42,28 +41,35 @@ function removeFirstTwo(list) {
}
```

## Hint 3

Exclude the first two elements of the `arr` array with `,,`.
## Spoiler Alert - Solution Ahead!
You can use random variables to omit the first two values:

```javascript
function removeFirstTwo(list) {
"use strict";
// change code below this line
const [,,...arr] = list; // change this
const [a, b, ...arr] = list;
thecodingaviator marked this conversation as resolved.
Show resolved Hide resolved
AdrianSkar marked this conversation as resolved.
Show resolved Hide resolved
// change code above this line
return arr;
}
```
## Solution 2:

## Spoiler Alert - Solution Ahead!
You can also exclude the first two elements of the `arr` array using `,,`.

```javascript
function removeFirstTwo(list) {
AdrianSkar marked this conversation as resolved.
Show resolved Hide resolved
"use strict";
// change code below this line
const [a, b, ...arr] = list;
const [,,...arr] = list; // change this
// change code above this line
return arr;
}
```

### Resources

- ["Destructuring assignment" - *MDN JavaScript reference*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment)