Skip to content

Instantly share code, notes, and snippets.

@AlphaNerd
Created May 13, 2017 03:35
Show Gist options
  • Save AlphaNerd/efbf20a6d75c084517fd9043850a2b99 to your computer and use it in GitHub Desktop.
Save AlphaNerd/efbf20a6d75c084517fd9043850a2b99 to your computer and use it in GitHub Desktop.
Move an item in Array from one position to another easily
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