This is a small snippet that gives Javascript arrays the (much-needed) ability to remove elements based on value. Example:
items = [1,2,3,3,4,4,5];
items.remove(3); // => [1,2,4,4,5]
Array.prototype.remove = function(elem) { | |
var ai = this.indexOf(elem); | |
while(ai > -1){ | |
this.splice(ai,1); | |
ai = this.indexOf(elem); | |
} | |
return this; | |
}; |
One thing to note, is that this modifies the original array, whereas your method returns a new array.
Your way:
My way:
To get my way working like yours you would have to copy the array first, via slice or concat: