Created
November 6, 2011 22:15
-
-
Save sunils34/1343649 to your computer and use it in GitHub Desktop.
moveItem - Repositioning an element within a Javascript 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
/** | |
* 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