How do I remove a particular element from an array in javascript? [duplicate]

2

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.

    
asked by jcardonan 16.09.2017 в 16:35
source

2 answers

2

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.

    
answered by 16.09.2017 / 16:40
source
1

You can use:


delete array[int];

Where array is the variable of the array and int the key to be deleted. Greetings.

    
answered by 16.09.2017 в 16:38