Skip to content

Instantly share code, notes, and snippets.

@zykadelic zykadelic/array.destroy.js
Last active Jan 27, 2016

What would you like to do?
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
You can’t perform that action at this time.