How to Remove a Specific Item from an Array in JavaScript ?
Given an Array, the task is remove specific item from an array in JavaScript. It means we have an array with N items, and remove a particular item from array.
Examples
Input: Arr = [ 10, 20, 30, 40, 50, 60 ] removeItem = 40 Output: [ 10, 20, 30, 50, 60 ]
Table of Content
Using splice() Method
The Array splice() method is used to remove an item from the array by its index. It modifies the original array.
Syntax
Array.splice( index, remove_count, item_list );
Example: Remove specific item 30 from given array [10, 20, 30, 40, 50] using splice() method.
function removeItem(array, itemToRemove) {
const index = array.indexOf(itemToRemove);
if (index !== -1) {
array.splice(index, 1);
}
console.log("Updated Array: ", array);
}
// Driver Code
const arr = [ 10, 20, 30, 40, 50 ];
// Remove item 30 from Array
removeItem( arr, 30 );
Output
Updated Array: [ 10, 20, 40, 50 ]
Using filter() Method
The Array filter() Method creates new array by excluding the item that you want to remove.
Syntax
array.filter( callback(element, index, arr), thisValue );
Example: Remove specific item 30 from array [10, 20, 30, 40, 50] using filter() method.
function removeItem(array, itemToRemove) {
return array.filter(
item => item !== itemToRemove);
}
// Driver code
const arr = [ 10, 20, 30, 40, 50 ];
// Remove item 30 from Array
console.log("Updated Array: ", removeItem(arr, 30));
Output
Updated Array: [ 10, 20, 40, 50 ]
Using slice() and indexOf() Methods
The indexOf() method finds the index of item which need to remove. If the item is found then it creates a new array using slice() method to extract elements of before and after the found item index. At last, use spread operator combine the sliced array.
function removeItem(array, itemToRemove) {
let index = array.indexOf(itemToRemove);
let newArr = index !== -1 ?
[...array.slice(0, index), ...array.slice(index + 1)] : fruits;
return newArr;
}
// Driver code
let arr = [ 10, 20, 30, 40, 50 ];
// Remove item 30 from Array
console.log(removeItem(arr, 30));
Output
[ 10, 20, 40, 50 ]
Using indexOf(), slice(), and concat() Methods
The indexOf()
method used to find the index of item to be removed. If the item is found then it creates a new array and use slice() method
to extract elements of before and after the found item index. At last, use concat() method to combine the sliced array.
For Example, given an array [ 10, 20, 30, 40, 50 ], and we have to remove item 30 from array. The indexOf() method find the index of item 30, which is 2. The array is then split into two parts using the slice() method: [10, 20] (elements before index 2) and [40, 50] (elements after index 2). These two sub-arrays are combined using the concat() method, resulting in [10, 20, 40, 50], effectively removing 30 from the array.
Example: Removing the item 30 from array using indexOf(), slice() and concat() methods.
function removeItem(array, itemToRemove) {
const index = array.indexOf(itemToRemove);
if (index !== -1) {
array = array.slice(0, index)
.concat(array.slice(index + 1));
}
return array;
}
// Driver code
const arr = [ 10, 20, 30, 40, 50 ];
// Remove item 30 from array
console.log("New Array: ", removeItem(arr, 30));
Output
New Array: [ 10, 20, 40, 50 ]