Created
August 25, 2015 00:17
-
-
Save chanified/e3dd8f3ad2cf6e29469a to your computer and use it in GitHub Desktop.
Remove from JS Array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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