2018-09-30 23:01:58 +01:00
---
id: 587d78b2367417b2b2512b10
title: Remove Items Using splice()
challengeType: 1
2019-08-05 09:17:33 -07:00
forumTopicId: 301166
2021-01-13 03:31:00 +01:00
dashedName: remove-items-using-splice
2018-09-30 23:01:58 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
Ok, so we've learned how to remove elements from the beginning and end of arrays using `shift()` and `pop()` , but what if we want to remove an element from somewhere in the middle? Or remove more than one element at once? Well, that's where `splice()` comes in. `splice()` allows us to do just that: **remove any number of consecutive elements** from anywhere in an array.
`splice()` can take up to 3 parameters, but for now, we'll focus on just the first 2. The first two parameters of `splice()` are integers which represent indexes, or positions, of the array that `splice()` is being called upon. And remember, arrays are *zero-indexed* , so to indicate the first element of an array, we would use `0` . `splice()` 's first parameter represents the index on the array from which to begin removing elements, while the second parameter indicates the number of elements to delete. For example:
2019-04-26 17:21:49 -07:00
2019-05-01 09:33:02 -07:00
```js
2019-04-26 17:21:49 -07:00
let array = ['today', 'was', 'not', 'so', 'great'];
array.splice(2, 2);
// remove 2 elements beginning with the 3rd element
// array now equals ['today', 'was', 'great']
```
2020-11-27 19:02:05 +01:00
`splice()` not only modifies the array it's being called on, but it also returns a new array containing the value of the removed elements:
2019-04-26 17:21:49 -07:00
2019-05-01 09:33:02 -07:00
```js
2019-04-26 17:21:49 -07:00
let array = ['I', 'am', 'feeling', 'really', 'happy'];
let newArray = array.splice(3, 2);
// newArray equals ['really', 'happy']
```
2020-11-27 19:02:05 +01:00
# --instructions--
We've initialized an array `arr` . Use `splice()` to remove elements from `arr` , so that it only contains elements that sum to the value of `10` .
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --hints--
2019-11-26 12:14:44 -08:00
2020-11-27 19:02:05 +01:00
You should not change the original line of `const arr = [2, 4, 5, 1, 7, 5, 2, 1];` .
2019-11-26 12:14:44 -08:00
2020-11-27 19:02:05 +01:00
```js
assert(
__helpers.removeWhiteSpace(code).match(/constarr=\[2,4,5,1,7,5,2,1\];?/)
);
```
`arr` should only contain elements that sum to `10` .
```js
assert.strictEqual(
arr.reduce((a, b) => a + b),
10
);
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
Your code should utilize the `splice()` method on `arr` .
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(__helpers.removeWhiteSpace(code).match(/arr\.splice\(/));
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
The splice should only remove elements from `arr` and not add any additional elements to `arr` .
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(
!__helpers.removeWhiteSpace(code).match(/arr\.splice\(\d+,\d+,\d+.*\)/g)
);
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --seed--
## --seed-contents--
2018-09-30 23:01:58 +01:00
```js
2019-11-26 12:14:44 -08:00
const arr = [2, 4, 5, 1, 7, 5, 2, 1];
2020-03-02 23:18:30 -08:00
// Only change code below this line
2018-09-30 23:01:58 +01:00
2020-03-02 23:18:30 -08:00
// Only change code above this line
2019-11-26 12:14:44 -08:00
console.log(arr);
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
# --solutions--
2018-09-30 23:01:58 +01:00
```js
2019-11-26 12:14:44 -08:00
const arr = [2, 4, 5, 1, 7, 5, 2, 1];
arr.splice(1, 4);
2018-09-30 23:01:58 +01:00
```