Often there is a requirement to delete a specific element from an array in javascript. This article will discuss removing either first or last or all occurrences of a particular element from a javascript array using simple methods and example illustrations.

Table of Contents:-

Javascript remove first occurrence of a specific element from an array

Javascript’s splice() method is used to modify the elements of an array. The splice() method can remove, replace or/and add new elements to the array.

Example:-

Remove first occurrence of 25 from the array [1,4,9,16,25,1,4,9,16,25]

Code:-

let  numArray = [1,4,9,16,25,1,4,9,16,25];
let element = 25;
let index = numArray.indexOf(element);
console.log("Array Elements Before Removing: " + numArray);
if (index > -1) {
    numArray.splice(index, 1);
}
console.log("Array Elements After Removing : " + numArray);

Output:-

Array Elements Before Removing: 1,4,9,16,25,1,4,9,16,25
Array Elements After Removing : 1,4,9,16,1,4,9,16,25

Explanation:-

  • The above code uses the splice() method to delete only 1 occurrence of element 25.
  • The index of 25 is found using the indexOf() method, and the value gets stored in the index variable.
  • This index then gets passed to the splice(index,1) method as an argument. Here, the splice() method will remove 1 (second argument) element starting from the index (first argument).

Javascript remove all occurrences of a specific element from an array

Example:-

Remove all occurrence of 25 from the array [1,4,9,16,25,1,4,9,16,25]

Using splice() :-

Function Code:-

function deleteAllItemInstances(_array, _item) {
    var i = 0;
    while (i < _array.length) {
      if (_array[i] === _item) {
        _array.splice(i, 1);
      } else {
        ++i;
      }
    }
    return _array;
  }

Usage:-

let numArray = [1,4,9,16,25,1,4,9,16,25]; 
console.log("Array Elements Before Removing: " + numArray);
deleteAllItemInstances(numArray,25);
console.log("Array Elements After Removing : " + numArray);

Output:-

Array Elements Before Removing: 1,4,9,16,25,1,4,9,16,25
Array Elements After Removing : 1,4,9,16,1,4,9,16

Explanation:-

  • The above code uses the splice() method to delete all occurrences of element 25.
  • All elements of the array are traversed using the while() loop, and when element 25 is found, the splice(i,1) is applied, which says to delete only 1 element from the index i. This process gets repeated till all the elements of the array are visited.

Using filter() :-

Code:-

let removeElements = [25]
let numArray = [1,4,9,16,25,1,4,9,16,25]
console.log("Array Elements Before Removing: " + numArray);
numArray = numArray.filter(element => !removeElements.includes(element))
console.log("Array Elements Before Removing: " + numArray);

Output:-

Array Elements Before Removing: 1,4,9,16,25,1,4,9,16,25
Array Elements After Removing : 1,4,9,16,1,4,9,16

Explanation:-

  • The above code uses the filter() method to delete all occurrences of element 25.
  • The removeElements array has only the elements which need to be removed. In our case, it’s the only element with a value of 25.
  • Then the filter() method is applied to all the elements of the numArray, which says to get only those elements that do not include elements of the removeElements array.

Javascript remove last occurrence of a specific element from an array

Example:-

Remove last occurrence of 25 from the array [1,4,9,16,25,1,4,9,16,25]

Code:-

let  numArray = [1,4,9,16,25,1,4,9,16,25];
let element = 25;
let index = numArray.lastIndexOf(element);
console.log("Array Elements Before Removing: " + numArray);
if (index > -1) {
    numArray.splice(index, 1);
}
console.log("Array Elements After Removing : " + numArray);

Output:-

Array Elements Before Removing: 1,4,9,16,25,1,4,9,16,25
Array Elements After Removing : 1,4,9,16,25,1,4,9,16

Explanation:-

  • The above code uses the splice() method to delete only 1 occurrence of element 25.
  • The last index of 25 is found using the lastIndexOf() method, and the value gets stored in the index variable.
  • This index then gets passed to the splice(index,1) method as an argument. Here, the splice() method will remove 1 (second argument) element starting from the index (first argument).

Read More:

I hope this article helped you to delete occurrences of a specific element from an array in javascript. Good Luck !!!