You can use the filter method here:
var elements = [1, 5, 5, 3, 5, 2, 4].filter(function(a){return a !== 5;});
Or, if you do not 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 = [] );
See MDN Documentation for filter
Alternatively, you can extend 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);
KooiInc May 03 '13 at 6:02 am 2013-05-03 06:02
source share