Skip to content

Instantly share code, notes, and snippets.

@ginotria
Created January 25, 2013 09:13
Show Gist options
  • Save ginotria/4632995 to your computer and use it in GitHub Desktop.
Save ginotria/4632995 to your computer and use it in GitHub Desktop.
Javascript: Moving an array element to another position in the array
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