How can you remove a specific item from an array?

Published July 30, 2021, Modified July 30, 2021

Quick summary

This tutorial will cover the .splice() and .indexOf() array methods and their application in removing a specific item from an array in JavaScript.

It's the year 3025. Humanity has finally got out of the solar system and is now exploring new frontiers. Unfortunately, JavaScript still has not got a .remove() method.

Let's start with a simple array. The contents or data types inside of it don't matter.

1const numbers = [1, 5, 2, 5, 3, 5]
2

Now, suppose we need to remove the number 2. Let's see how to do that:

Example 1 - Remove one occurrence

1const removeItem = (arr, val) => {
2 const index = arr.indexOf(val)
3 if (index > -1) {
4 arr.splice(index, 1)
5 }
6 return arr
7}
8
9removeItem(numbers, 2)
10
11console.log(numbers)
12// [1, 5, 5, 3, 5]
13

So, as you can see, we have declared a new function called removeItem().

This function takes two parameters:

  • The array that we want to modify
  • The value we have to remove

First, we want to see if that value exists in the array. We do that with the .indexOf() array method.

The .indexOf() method applied to any array will return the index of a given value if it exists in the array. If it doesn't, it will return -1.

After that, we create a condition that checks that the index value does exists.

If it exists, we apply the .splice() method to remove it from the array, and once everything is done, we return the resultant array.

.splice()

.splice() is a powerful method that allows us to change the contents of an array by removing and adding elements to it.

Remember. It does so by mutating the original array.

In this case, we passed two parameters:

  • The start index from where we will start making changes
  • The delete count

That means that in our use of the .splice() method, we are just telling it to remove the element on the given index.

What if we want to remove all the matching items?

Example 2 - Remove all occurrences V1

Let's see how to do that:

1const removeAllItems = (arr, val) => {
2 let i = 0
3 while (i < arr.length) {
4 if (arr[i] === val) {
5 arr.splice(i, 1)
6 } else {
7 ++i
8 }
9 }
10 return arr
11}
12
13removeAllItems(numbers, 5)
14
15console.log(numbers)
16// [1, 3] notice the missing 2. Remember that .splice() mutates the array and we already removed the number 2.
17

So, here we are applying .splice() in the same way. The only difference is that we are iterating over the array to work over all the elements and see if they are occurrences of the item we want to delete.

Now, notice that we are not using a for loop. The only reason for that is that we don't want to increment our index if we remove it from the array. That would make our function skip some elements, and we don't want that.

Let me show you another way of removing all the occurrences of an item in an array.

Example 3 - Remove all occurrences V2

1const numbers2 = [1, 2, 3, 2, 4]
2
3const removeAllItems2 = (arr, val) => {
4 return arr.filter(item => item !== val)
5}
6
7const newNumbers = removeAllItems2(numbers2, 2)
8
9console.log(numbers2)
10// [1, 2, 3, 2, 4]. No mutation
11
12console.log(newNumbers)
13// [1, 3, 4]
14

This is my personal favorite. We are using the .filter() method to return a new array.

The filter method iterates over all the elements in an array and, in this case, if the element is not equal to the value we want to remove, it will add it to a new array, and after it finishes, it will return the new array.

This is an excellent option if you don't want to mutate the original array.

Wrapping up

So there you go. We just saw three methods to remove a specific item for an array, either to remove one occurrence or all of them. I hope this helps you on your development journey. Thanks for reading!

author image

Hi, I'm Leo Cuéllar. I'm a Software Engineer, technical interviewer, and technical writer. I'm also the founder of Devcore. I enjoy writing about front-end and job interview topics.

🌌 Join the Devcore

Subscribe and receive a weekly email with the most popular articles about modern front-end web development, the latest interview questions and the most relevant job opportunities.

No spam. Unsubscribe anytime.