I have an integer array, I'm using the .push()
method to add elements to it. Is there a simple way to remove an element from the array? THE equivalent of something like:
array.remove(int);
I must use pure javascript without frameworks.
I have an integer array, I'm using the .push()
method to add elements to it. Is there a simple way to remove an element from the array? THE equivalent of something like:
array.remove(int);
I must use pure javascript without frameworks.
First find the index of the item you want to remove:
var array=[2,5,9];
var index = array.indexOf(5);
Then remove the element with splice
if(index > -1) {
array.splice(index,1);
}
The second parameter of the splice is the number of elements to be removed. Do not expect the array to do the splice and return a new array containing elements that have been removed.
You can use:
delete array[int];
Where array is the variable of the array and int the key to be deleted. Greetings.