Skip to content

Instantly share code, notes, and snippets.

@timwis
Last active October 6, 2016 10:05
Show Gist options
  • Save timwis/1d09e69810b21bb87741927f4b61f841 to your computer and use it in GitHub Desktop.
Save timwis/1d09e69810b21bb87741927f4b61f841 to your computer and use it in GitHub Desktop.
// 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