Skip to content

Instantly share code, notes, and snippets.

@sunils34
Created November 6, 2011 22:15
Show Gist options
  • Save sunils34/1343649 to your computer and use it in GitHub Desktop.
Save sunils34/1343649 to your computer and use it in GitHub Desktop.
moveItem - Repositioning an element within a Javascript array
/**
* moveItem
*
* Remove the element positioned at oldIdx,
* and insert this back into the array positioned at newIdx
*
* This does not do any bounds checking on the inputs
*
* @param array
* @param integer old index
* @param integer new index
* @return array modified array
*
*/
function moveItem(arr, oldIdx, newIdx) {
return arr.splice(newIdx, 0, arr.splice(oldIdx, 1)[0]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment