How do I remove a particular element from an array in JavaScript?
I have an array of numbers array = [5,9,5,10]; , and I'm using the .pop()
method to remove elements from it.
but only last element from array is removing..
Answer (1)
this your solution use indexOf() and array.splice() method in javascript.
var array = [2, 5, 9];
console.log(array);
var index = array.indexOf(5);
if (index > -1) {
array.splice(index, 1);
}
// array = [2, 9]
alert(array);