2018-10-12 15:37:13 -04:00
---
2019-05-08 10:30:24 -04:00
title: Use Destructuring Assignment with the Rest Parameter to Reassign Array Elements
2018-10-12 15:37:13 -04:00
---
2019-07-24 00:59:27 -07:00
# Use Destructuring Assignment with the Rest Parameter to Reassign Array Elements
---
## Problem Explanation
2019-05-08 10:30:24 -04:00
Remember that the rest parameter allows for variable numbers of arguments. In this challenge, you have to get rid of the first two elements of an array.
2019-03-29 17:40:13 +01:00
2019-07-24 00:59:27 -07:00
---
## Hints
### Hint 1
2018-10-12 15:37:13 -04:00
Assign the first two elements to two random variables.
2019-07-24 00:59:27 -07:00
### Hint 2
2018-10-12 15:37:13 -04:00
Set the remaining part of the array to `...arr` .
2019-07-24 00:59:27 -07:00
### Hint 3
2018-10-12 15:37:13 -04:00
2019-03-29 17:40:13 +01:00
Use destructuring to create the `arr` variable:
2018-10-12 15:37:13 -04:00
```javascript
function removeFirstTwo(list) {
"use strict";
// change code below this line
const [arr] = list; // change this
// change code above this line
return arr;
}
```
2019-07-24 00:59:27 -07:00
### Hint 4:
2018-10-12 15:37:13 -04:00
2019-03-29 17:40:13 +01:00
Spread the `list` parameter values into `arr` .
2018-10-12 15:37:13 -04:00
```javascript
function removeFirstTwo(list) {
"use strict";
// change code below this line
const [...arr] = list; // change this
// change code above this line
return arr;
}
2019-05-08 10:30:24 -04:00
```
2018-10-12 15:37:13 -04:00
2019-03-29 17:40:13 +01:00
You can use random variables to omit the first two values:
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
---
## Solutions
< details > < summary > Solution 1 (Click to Show/Hide)< / summary >
2018-10-12 15:37:13 -04:00
```javascript
2019-07-24 00:59:27 -07:00
const source = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
2018-10-12 15:37:13 -04:00
function removeFirstTwo(list) {
"use strict";
// change code below this line
2019-05-08 10:30:24 -04:00
const [a, b, ...arr] = list;
2018-10-12 15:37:13 -04:00
// change code above this line
return arr;
}
2019-03-29 17:40:13 +01:00
const arr = removeFirstTwo(source);
console.log(arr); // should be [3,4,5,6,7,8,9,10]
console.log(source); // should be [1,2,3,4,5,6,7,8,9,10];
2018-10-12 15:37:13 -04:00
```
2019-03-29 17:40:13 +01:00
2019-07-24 00:59:27 -07:00
You can also exclude the first two elements of the `arr` array using `,,` .
#### Relevant Links
2019-03-29 17:40:13 +01:00
- ["Destructuring assignment" - *MDN JavaScript reference* ](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment )
2019-07-24 00:59:27 -07:00
< / details >