Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chanified/e3dd8f3ad2cf6e29469a to your computer and use it in GitHub Desktop.
Save chanified/e3dd8f3ad2cf6e29469a to your computer and use it in GitHub Desktop.
Remove from JS Array
// Array Remove - By John Resig (MIT Licensed)
//http://ejohn.org/blog/javascript-array-remove/
Array.prototype.remove = function(from, to) {
var rest = this.slice((to || from) + 1 || this.length);
this.length = from < 0 ? this.length + from : from;
return this.push.apply(this, rest);
};
and here's some examples of how it could be used:
// Remove the second item from the array
array.remove(1);
// Remove the second-to-last item from the array
array.remove(-2);
// Remove the second and third items from the array
array.remove(1,2);
// Remove the last and second-to-last items from the array
array.remove(-2,-1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment