Last active
October 6, 2016 10:05
-
-
Save timwis/1d09e69810b21bb87741927f4b61f841 to your computer and use it in GitHub Desktop.
This file contains 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
// Move item in an array immutably | |
function moveItem (array, fromIndex, toIndex) { | |
console.log(`moving from ${fromIndex} to ${toIndex}`) | |
const arrayCopy = array.slice() | |
const item = arrayCopy[fromIndex] | |
arrayCopy.splice(fromIndex, 1) // remove field that's moving | |
arrayCopy.splice(toIndex, 0, item) // add it back | |
return arrayCopy | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment