Skip to content

Instantly share code, notes, and snippets.

@ozee31
Last active June 18, 2020 11:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ozee31/0e8e6f3001ae987ab42c to your computer and use it in GitHub Desktop.
Save ozee31/0e8e6f3001ae987ab42c to your computer and use it in GitHub Desktop.
Supprime des éléments d'un tableau
/**
* Remove all occurrences of a given value from an array
* @param {mixed} val : value to delete
*/
Array.prototype.unset = function(val){
var index;
while ( (index = this.indexOf(val)) !== -1 ) {
this.splice(index,1);
}
}
/** EXEMPLES */
test = ['foo', 'bar', 'foo', 'ozee31'];
test.unset('bar');
console.log(test); /* Array [ "foo", "foo", "ozee31" ] */
test = ['foo', 'bar', 'foo', 'ozee31'];
test.unset('foo');
console.log(test); /* Array [ "bar", "ozee31" ] */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment