I have an array of integers, which I'm using the .push() element to add to.

Is there a simple way to remove a specific element from a javascript array? The equivalent of something like array.remove(int);

I have to use good ol' fashion javascript - no frameworks allowed.

share|improve this question

6 Answers

up vote 83 down vote accepted

First, find the index of the element you want to remove:

var array = [2, 5, 9];
var index = array.indexOf(5);

Then remove it with splice:

array.splice(index, 1);

The second parameter of splice is the number of elements to remove. Note, splice() modifies the array in place and returns an array with the elements you remove.

share|improve this answer
4  
Good solution, but some browsers don't suppoer array.indexOf – Peter Olson Apr 23 '11 at 22:25
2  
The second argument to the splice function is how many elements to remove. – tomwadley Apr 23 '11 at 22:25
3  
@Peter, yes you might be right. This article explains more and has a workaround for incompatible browsers: developer.mozilla.org/en/JavaScript/Reference/Global_Objects/… – tomwadley Apr 23 '11 at 22:28
This worked perfectly for my need (Appcelerator JavaScript framework for iPhone development) and seems elegant - thanks! – Walker Apr 23 '11 at 22:35
delete array[i]

or

array.splice(i,1);

I may have misunderstood your question, and this might be what you want instead:

for(var i = array.length; i>=0; i--){
  if(array[i]==number){
   array.splice(i,1);
   break;
  }
}
share|improve this answer
This worked as well, I chose to go with the indexOf function in the end though. Thanks for the help! – Walker Apr 23 '11 at 22:35
2  
delete is not the correct way to remove an element from an array! – Felix Kling Jan 27 at 15:30
@FelixKling It depends, it works if you want to make it so that array.hasOwnProperty(i) returns false and have the element at that position return undefined. But I'll admit that that's not a very common thing to want to do. – Peter Olson Jan 27 at 15:36
delete will not update the length of the array neither really erases the element, only replaces it with the special value undefined. – diosney Feb 17 at 3:44

Be aware that when you use delete for an array than you could get wrong results for anArray.length. In other words, delete would remove the element but not update the value of length property.

Be careful when you use delete for an array. It is good for deleting attributes of objects but not so good for arrays. It is better use splice for arrays.

share|improve this answer
Array.prototype.remByVal = function(val) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] === val) {
            this.splice(i, 1);
            i--;
        }
    }
    return this;
}
//Call like
[1, 2, 3, 4].remByVal(3);
share|improve this answer
2  
I'm not a big fan of this approach. If you end up using different libraries or frameworks, they can end up conflicting with each other. – Charlie Kilian Apr 23 '11 at 22:30
This worked as well, I chose to go with the indexOf function in the end though. Thanks for the help! – Walker Apr 23 '11 at 22:35
1  
Bad idea, see this post: stackoverflow.com/questions/948358/array-prototype-problem – MMeah Jul 9 '12 at 22:10

Depends on whether you want to keep an empty spot or not.

If you do want an empty slot, delete is fine:

delete array[ index ];

If you don't, you should use the splice method:

array.splice( index, 1 );

And if you need the value of that item, you can just store the return value:

var value = array.splice( index, 1 );

In case you want to do it in some order, you can use array.pop( ) for the last one or array.shift( ) for the first one (and both return the value of the item too).

And if you don't know the index of the item, you can use array.indeOf( item ) to get it (in a if() to get one item or in a while() to get all of them). array.indexOf( item ) returns either the index or -1 if not found.

share|improve this answer

You can always delete the specific element and filter out the array. It might need an extension of the array object for browsers that don't implement the filter method but in the long term its easier since all you do is this:

var my_array = [1,2,3,4,5,6];
delete my_array[4];
console.log(my_array.filter(function(a){return typeof a !== 'undefined';}));

Should display [1, 2, 3, 4, 6]

share|improve this answer

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.