How to Remove Element from an Array: JavaScript

Removing elements from a JavaScript array is a common programming paradigm that developers often run into. As with a lot of things JavaScript, this isn’t as simple as it probably should be. There are actually several ways to remove one or more elements from an array so let’s go through each of them one by one.

Removing One Element Using splice():

The splice() method is a versatile way of removing, replacing, and/or adding elements in an array. It works similarly to splice() functions in other languages. Basically, you take an array and selectively remove portions of it. The inputs to the splice() function are the index point to start at and the number of elements to remove. Also, remember that arrays are zero-indexed in JavaScript.

["AA", "AB", "AC", "AD"]                                                                                                             list.splice(2, 1)                                                                                                                                   // Starting at index position 2, remove one element                                                                                 ["AA", "AB", "AD"]                                                                                                                    list.splice(2,2)                                                                                                                                   // Starting at index position 2, remove two elements                                                                                           output: ["AA", "AB"]

Removing One Element Using pop():

The array methods push() and pop() work on the the end of an array. The terms push() and pop() come from the idea of a stack and queue. This implements the idea of a Last-In-First-Out data structure (LIFO). The push() method will ADD an element to the array and the pop() method will remove one. To remove the last element of an array:

["AA", "AB", "AC", "AD"]                                                                                                                    list.pop()                                                                                                                                    output: ["AA", "AB", "AC"]

Removing One Element Using shift():

The array methods shift() and unshift() work on the beginning of an array instead of the end of an array, as is the case with push() and pop(). The shift() command will remove the first element of the array and the unshift() command will add an element to the beginning of the array.

["AA", "AB", "AC", "AD"]                                                                                                                  list.shift()                                                                                                                                  output: ["AB", "AC", "AD"]

Searching and Removing a Specific Element by Value:

The indexOf() command returns the first index at which a given element can be found in the array, or -1 if it is not present. This can be used along with splice() to search for an element and then remove it, even if you don’t know where it is in the array.

Let’s remove the "AC" element:

["AA", "AB", "AC", "AD"]                                                                                         list.splice( list.indexOf('AC'), 1 );                                                                                                               // Find the index position of "AC," then remove one element from that position                                                                 output: ["AA", "AB", "AD"]

Removing Multiple Specific Elements

Let’s add an extra "AC" element to our array, and then remove all occurrences of "AC":

["AA", "AB", "AC", "AC", "AD"]                                                                                               for( var i = list.length-1; i--;){                                                                          if ( list[i] === 'AC') list.splice(i, 1);}                                                                                                    output: ["AA", "AB", "AD"]

Note that we could also have used the filter()method for this operation, but that would have created a new array, as filter() does not mutate the array on which it is called. Perform a lot of filtering using the filter() method might clean up your code quite a bit.



Was this article helpful?



You are freelancing Ninja?

Try Toogit Instant Connect today, The new virtual assistant for your freelancing world.