Skip to content

Instantly share code, notes, and snippets.

@loic-moriame
Created August 20, 2013 10:33
Show Gist options
  • Save loic-moriame/6279850 to your computer and use it in GitHub Desktop.
Save loic-moriame/6279850 to your computer and use it in GitHub Desktop.
Add function "array.unset(value, [global])" to remove a value from a one-dimensional's array. Can remove the first occurence or all
Array.prototype.unset = function(value, global){
var index = this.indexOf(value),
global = global || false;
while(index > -1) {
this.splice(index, 1);
index = global ? this.indexOf(value) : -1;
}
}
var a = ['Robin', 'Batman', 'Wolverine', 'Spiderman', 'Robin']
a.unset('Robin');
console.log(a);
//[ 'Batman', 'Wolverine', 'Spiderman', 'Robin' ]
var a = ['Robin', 'Batman', 'Wolverine', 'Spiderman', 'Robin']
a.unset('Robin', true);
console.log(a);
// [ 'Batman', 'Wolverine', 'Spiderman' ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment