removal
array
javascript

How can I remove a specific item from an array in JavaScript?

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

To remove a specific item from an array in JavaScript, you have several methods to choose from. Here are the most common and effective ones:

1. Using splice() Method

The splice() method can be used to remove an item from an array at a specific index.

javascript
1let array = [1, 2, 3, 4, 5];
2let index = array.indexOf(3); // Find the index of the item to remove
3
4if (index !== -1) {
5  array.splice(index, 1); // Remove the item at the specified index
6}
7
8console.log(array); // Output: [1, 2, 4, 5]
9

2. Using filter() Method

The filter() method creates a new array with all elements that pass the test implemented by the provided function. You can use this to exclude the specific item you want to remove.

javascript
1let array = [1, 2, 3, 4, 5];
2let itemToRemove = 3;
3
4array = array.filter(item => item !== itemToRemove);
5
6console.log(array); // Output: [1, 2, 4, 5]
7

3. Using findIndex() and splice()

If you want to remove an item based on a condition or a more complex criterion, you can combine findIndex() with splice().

javascript
1let array = [
2  { id: 1, name: 'John' },
3  { id: 2, name: 'Jane' },
4  { id: 3, name: 'Joe' }
5];
6
7let index = array.findIndex(item => item.id === 2); // Find index of item with id 2
8
9if (index !== -1) {
10  array.splice(index, 1); // Remove the item at the specified index
11}
12
13console.log(array);
14// Output: [{ id: 1, name: 'John' }, { id: 3, name: 'Joe' }]
15

4. Using for Loop and splice()

If you need to remove multiple items or perform additional logic during removal, you can use a for loop.

javascript
1let array = [1, 2, 3, 4, 5];
2let itemsToRemove = [2, 4];
3
4for (let i = 0; i < itemsToRemove.length; i++) {
5  let index = array.indexOf(itemsToRemove[i]);
6  if (index !== -1) {
7    array.splice(index, 1);
8  }
9}
10
11console.log(array); // Output: [1, 3, 5]
12

5. Using reduce() Method

The reduce() method can also be used to build a new array without the specific item.

javascript
1let array = [1, 2, 3, 4, 5];
2let itemToRemove = 3;
3
4array = array.reduce((result, item) => {
5  if (item !== itemToRemove) {
6    result.push(item);
7  }
8  return result;
9}, []);
10
11console.log(array); // Output: [1, 2, 4, 5]
12

Summary

  • splice() is ideal for removing an item by its index.
  • filter() is useful for removing items by value or condition.
  • findIndex() + splice() is good for removing items based on more complex conditions.
  • reduce() can be used to create a new array excluding the item(s) to be removed.

Each method has its use cases and can be chosen based on your specific needs and the complexity of the condition for removing items from the array.


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.