DEV Community

Avnish
Avnish

Posted on

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

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]
Enter fullscreen mode Exit fullscreen mode

Explanation:

  1. We have an array array1 containing elements [1, 2, 3, 4, 5].
  2. We want to remove the item 3.
  3. We find the index of the item using indexOf() method.
  4. If the item is found (index is not -1), we remove it using splice() method by specifying the index and number of elements to remove.
  5. 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]
Enter fullscreen mode Exit fullscreen mode

Explanation:

  1. We have an array array2 containing elements [10, 20, 30, 40, 50].
  2. We want to remove the item 30.
  3. We use filter() method to create a new array excluding the item to be removed.
  4. 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']
Enter fullscreen mode Exit fullscreen mode

Explanation:

  1. We have an array array3 containing elements ['a', 'b', 'c', 'd', 'e'].
  2. We directly specify the index (2) of the item to be removed ('c') and remove it using splice() method.
  3. 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']
Enter fullscreen mode Exit fullscreen mode

Explanation:

  1. We have an array array4 containing elements ['apple', 'banana', 'orange', 'grape'].
  2. We want to remove the item 'banana'.
  3. 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.
  4. 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]
Enter fullscreen mode Exit fullscreen mode

Explanation:

  1. We have an array array5 containing elements [100, 200, 300, 400, 500].
  2. We want to remove multiple items [200, 400].
  3. We iterate through each item to be removed using forEach() method.
  4. For each item, we find its index using indexOf() method and remove it using splice() if found.
  5. The output is the modified array [100, 300, 500].

Top comments (0)