Created
May 13, 2017 03:35
-
-
Save AlphaNerd/efbf20a6d75c084517fd9043850a2b99 to your computer and use it in GitHub Desktop.
Move an item in Array from one position to another easily
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.prototype.move = function (old_index, new_index) { | |
if (new_index >= this.length) { | |
var k = new_index - this.length; | |
while ((k--) + 1) { | |
this.push(undefined); | |
} | |
} | |
this.splice(new_index, 0, this.splice(old_index, 1)[0]); | |
return this; // for testing purposes | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment