Skip to content
the poor coder

js remove item from array by value

Poor Man
Published
Reading time
3 min read
On this page

Removing an item from an array using JavaScript#

JavaScript is a powerful programming language that is commonly used for web development. One of the most common tasks in JavaScript is removing an item from an array. There are several ways to do this, but the most common and efficient way is to use the splice() method.

The splice() method#

The splice() method is a built-in method in JavaScript that allows you to add or remove items from an array at a specified index. The general syntax of the splice() method is:

Text
array.splice(start, deleteCount, item1, item2, ...)
  • start: The index at which to start changing the array.
  • deleteCount: An integer indicating the number of old array elements to remove.
  • item1, item2, ...: The elements to add to the array, beginning at the start index. If you don't specify any elements, splice() will only remove elements from the array.

To remove an item from an array using splice(), you need to know the index of the item you want to remove. Once you have that index, you can use the following code:

JavaScript
let myArray = ['apple', 'banana', 'orange', 'pear'];
let index = myArray.indexOf('banana');
if (index > -1) {
  myArray.splice(index, 1);
}

In this example, we have an array called myArray that contains four items. We want to remove the item 'banana' from the array, so we use the indexOf() method to find the index of the item. If the index is greater than -1 (which means it was found), we use the splice() method to remove the item at that index.

The filter() method#

Another way to remove an item from an array is to use the filter() method. This method creates a new array with all elements that pass the test implemented by the provided function. The general syntax of the filter() method is:

Text
array.filter(callback(element[, index[, array]])[, thisArg])
  • callback: A function that accepts up to three arguments. The filter() method calls the callback function one time for each element in the array.
  • element: The current element being processed in the array.
  • index: Optional. The index of the current element being processed in the array.
  • array: Optional. The array filter was called upon.
  • thisArg: Optional. Value to use as this when executing callback.

To remove an item from an array using filter(), you need to create a function that returns a new array without the item you want to remove. Here's an example:

JavaScript
let myArray = ['apple', 'banana', 'orange', 'pear'];
let newArray = myArray.filter(function(item) {
  return item !== 'banana';
});

In this example, we have an array called myArray that contains four items. We want to remove the item 'banana' from the array, so we create a new array called newArray using the filter() method. The function inside the filter() method checks if each item in the array is not equal to 'banana'. If it is not equal, the item is added to the new array. If it is equal, the item is not added to the new array.

Conclusion#

Both the splice() method and the filter() method can be used to remove items from an array in JavaScript. The splice() method is more efficient for removing single items, while the filter() method is more efficient for removing multiple items or complex conditions. It's important to choose the right method for your specific needs to ensure optimal performance.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC