You can remove a specific item from an array in JavaScript using various methods such as splice()
, filter()
, or by finding the index of the item and using splice()
. Below are 5 examples demonstrating each method along with explanations and outputs:
Example 1: Using splice()
// Remove a specific item from an array using splice()
let array1 = [1, 2, 3, 4, 5];
const itemToRemove = 3;
const index = array1.indexOf(itemToRemove);
if (index !== -1) {
array1.splice(index, 1);
}
console.log("Example 1:", array1); // Output: [1, 2, 4, 5]
Explanation:
- We have an array
array1
containing elements[1, 2, 3, 4, 5]
. - We want to remove the item
3
. - We find the index of the item using
indexOf()
method. - If the item is found (index is not
-1
), we remove it usingsplice()
method by specifying the index and number of elements to remove. - The output is the modified array
[1, 2, 4, 5]
.
Example 2: Using filter()
// Remove a specific item from an array using filter()
let array2 = [10, 20, 30, 40, 50];
const itemToRemove2 = 30;
array2 = array2.filter(item => item !== itemToRemove2);
console.log("Example 2:", array2); // Output: [10, 20, 40, 50]
Explanation:
- We have an array
array2
containing elements[10, 20, 30, 40, 50]
. - We want to remove the item
30
. - We use
filter()
method to create a new array excluding the item to be removed. - The output is the modified array
[10, 20, 40, 50]
.
Example 3: Using splice()
directly with index
// Remove a specific item from an array using splice() with direct index
let array3 = ['a', 'b', 'c', 'd', 'e'];
const indexToRemove = 2; // Index of 'c'
array3.splice(indexToRemove, 1);
console.log("Example 3:", array3); // Output: ['a', 'b', 'd', 'e']
Explanation:
- We have an array
array3
containing elements['a', 'b', 'c', 'd', 'e']
. - We directly specify the index (
2
) of the item to be removed ('c'
) and remove it usingsplice()
method. - The output is the modified array
['a', 'b', 'd', 'e']
.
Example 4: Using spread operator and slice()
// Remove a specific item from an array using spread operator and slice()
let array4 = ['apple', 'banana', 'orange', 'grape'];
const itemToRemove4 = 'banana';
array4 = [...array4.slice(0, array4.indexOf(itemToRemove4)), ...array4.slice(array4.indexOf(itemToRemove4) + 1)];
console.log("Example 4:", array4); // Output: ['apple', 'orange', 'grape']
Explanation:
- We have an array
array4
containing elements['apple', 'banana', 'orange', 'grape']
. - We want to remove the item
'banana'
. - We use the spread operator (
...
) to concatenate two slices of the array: the elements before the item to be removed and the elements after the item to be removed. - The output is the modified array
['apple', 'orange', 'grape']
.
Example 5: Using splice()
to remove multiple items
// Remove multiple specific items from an array using splice()
let array5 = [100, 200, 300, 400, 500];
const itemsToRemove = [200, 400];
itemsToRemove.forEach(item => {
const index = array5.indexOf(item);
if (index !== -1) {
array5.splice(index, 1);
}
});
console.log("Example 5:", array5); // Output: [100, 300, 500]
Explanation:
- We have an array
array5
containing elements[100, 200, 300, 400, 500]
. - We want to remove multiple items
[200, 400]
. - We iterate through each item to be removed using
forEach()
method. - For each item, we find its index using
indexOf()
method and remove it usingsplice()
if found. - The output is the modified array
[100, 300, 500]
.
Top comments (0)