Remove an Item From an Array in JavaScript

Removing an item from an Array in JavaScript is a common task that we all come across at some time as a developer. You have various ways to do this depending upon what you are trying to accomplish. Whether you want to remove an item by its index or value there is a method available. In this article, we will explore all the commonly used methods with practical examples.

1. Remove an Item From Array Using the splice() Method

It comes into play when you want to add or remove an item from any index in the array. For this particular article, we will focus on removing items. If you want to learn about how to add items in the array then you can read this article on ways to add elements in an array.

Let’s take a look at some of the examples using the splice()  method –

1. Remove an Element before index 2, and insert “Cookie”

code explanation:
1. We have an array with four elements.
2. We are adding  “cookie” at index 2 which is specified as the first parameter.
3. The second parameter 1 specifies that we are removing one element.

2. Remove an Element Before Index 2, and Insert “Cookie” and “Waffles”

code explanation:
1. We are adding “cookie” and “waffles” at index 2.
2. We are removing one element from the array and it will be the right next element after index 2.

3. Remove Last Element using the Splice() method.

Here we are not specifying the index that we want to remove. This method comes in handy when we have a large number of elements in the array. The array.length method automatically calculates the length of the array. In the code we are doing a similar operation, checking the length of the array and removing the last element “donut” with “cookie”;

2. Removing an Item by Value Using filter() Method

As the name suggests, the filter()  method creates a new array with all the elements passed through the test that has been set in the function. Unlike other arrays such as splice() ; it does not mutate the array, instead it creates a new array. In a nutshell, it creates a shallow copy of the portion of the given array by implementing the filtering as per given instructions.

Let’s take a look at an example of the filter()  method –

In this example, we are filtering the sweetDessert array to to filter out the “icecream” value from the array. Note that the output array does not contain the “icecream” value.

3. Removing the First or Last Item Using shift() or pop()

We have understood how to filter arrays and change array items at any index. It’s time to understand how we can remove the first and the last elements of an array. In the splice function, we have seen that we can replace elements at any index.

But what if we just want to remove the first and the last element without replacing it with a new element? To accomplish this we have two methods with the first one being shift()  and the second one being pop() .

Removing the first item

If you want to remove the first item of the array, you can use the shift()  method. It changes the length of the array as the array gets shortened. Here is an example of the same.

Removing the last item

Alternatively, if you want to remove the last element of the array you can use the pop()  method. It modifies the array and returns an array with a new length but this time it’s the last element that is removed from the array.

4. Remove an Item from an Array in JavaScript Using indexOf() method

There are situations when you know the value of an item but not the index, in such cases you can combine indexOf()  and splice()  methods to remove an item from the array. Let us understand this with an example where we will remove an item from the array where we don’t know its index.

In the example we already know the value of the item – “orange”. Now we are checking its index in the array . After getting the index, we use splice() method to remove the item.

5. Using reduce() to Remove Items Conditionally

The reduce()  method can be used to remove items from an array based on a condition. This approach is more advanced and allows for complex transformations. For instance, you have to specify a specific condition before it gets implemented. In the example below we are adding a condition where it checks a banana in the array and then removes it. The point here is that we are not replacing the item, but instead removing it from the list. It means it will return a new array with a new length.

Let’s see how this works –

We are running a loop in the array that checks if it’s fruit or not. This goes the same for all the items in the array. Once it finds the specified item it removes that from the array and returns a new array excluding that specified one.

Conclusion

In JavaScript we have various ways to remove items from an array, each method has its use cases depending on what you are trying to accomplish. Whether you want to remove an item from the beginning or end from any specific index. The above-provided methods will do the work for you.
We have tried to cover various ways to remove an item from an array in JavaScript. You can run through it multiple times to understand deeply and don’t forget to practice with your examples. If you liked this article, you can check out our blog where we post regularly about tips, tricks and ah ha! moments of WordPress and JavaScript.

Meet the Author

Vicky Bhandari

Vicky is an enthusiast WordPress developer and a technical writer with more than 5 years of experience in WordPress development. Besides WordPress, Vicky's passions include bikes and is tech fanatic.

Meet the Author

Vicky Bhandari

Vicky is an enthusiast WordPress developer and a technical writer with more than 5 years of experience in WordPress development. Besides WordPress, Vicky's passions include bikes and is tech fanatic.

Leave a Reply

Your email address will not be published. Required fields are marked *