Remove An Element From An Array In JavaScript
Her we conside how to remove an element from an array in JavaScript.
Lets start with one example here.
Suppose inside Script region, we have an array as shown here.
var array = [4, 8, 12, 16, 20];
Now, we wants to remove 12 from above array.
The best way is that we can make use of array.indexOf() method.
By using indexOf method, we can find the index of an element.
Here the index of element 12 is 2.
And then we have to make use of array.splice()
array.splice(startIndex, deleteCount)
startIndex is, Index at which to start changing the array (with origin 0).
deleteCount is, An integer indicating the number of old array elements to be removed.
In the above case, splice can be used as shown here.
array.splice(2, 1);
Inside array, from index 2, remove one element which is 12.
By this way we can remove an element from an array in JavaScript.