You could use the filter
method here:
var elements = [1, 5, 5, 3, 5, 2, 4].filter(function(a){return a !== 5;});
//=> elements now [1,3,2,4]
Or if you don't want to touch elements
:
var elementsfiltered
,elements = [1, 5, 5, 3, 5, 2, 4]
.filter( function(a){if (a!==5) this.push(a); return true;},
elementsfiltered = [] );
//=> elementsfiltered = [1,3,2,4], elements = [1, 5, 5, 3, 5, 2, 4]
See MDN documentation for filter
Alternatively you can extend the Array.prototype
Array.prototype.remove = Array.prototype.remove || function(val){
var i = this.length;
while(i--){
if (this[i] === val){
this.splice(i,1);
}
}
};
var elements = [1, 5, 5, 3, 5, 2, 4];
elements.remove(5);
//=> elements now [1,3,2,4]