Skip to content

Instantly share code, notes, and snippets.

@zykadelic
Last active January 27, 2016 21:08
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zykadelic/5069236 to your computer and use it in GitHub Desktop.
Save zykadelic/5069236 to your computer and use it in GitHub Desktop.
Destroy all occurrences of the argument from an array. Returns the destroyed element.
Array.prototype.destroy = function(obj){
// Return null if no objects were found and removed
var destroyed = null;
for(var i = 0; i < this.length; i++){
// Use while-loop to find adjacent equal objects
while(this[i] === obj){
// Remove this[i] and store it within destroyed
destroyed = this.splice(i, 1)[0];
}
}
return destroyed;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment