Skip to content

Instantly share code, notes, and snippets.

@marioluevanos
Last active July 23, 2018 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marioluevanos/fb6695187f3ec6b06e8ebcbc4d002f3b to your computer and use it in GitHub Desktop.
Save marioluevanos/fb6695187f3ec6b06e8ebcbc4d002f3b to your computer and use it in GitHub Desktop.
Mutates an array to a new index location
const reOrderArray = (array, oldIndex, newIndex) => {
if ( array.length === 0 ) return array
while (oldIndex < 0) {
oldIndex += array.length
}
while (newIndex < 0) {
newIndex += array.length
}
if (newIndex >= array.length) {
var k = newIndex - array.length
while ((k--) + 1) {
array.push(undefined)
}
}
array.splice(newIndex, 0, array.splice(oldIndex, 1)[0])
return array
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment