Skip to content

Instantly share code, notes, and snippets.

@autr
Created April 27, 2021 15:42
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 autr/143d839163e637b25c703dd2a429e32a to your computer and use it in GitHub Desktop.
Save autr/143d839163e637b25c703dd2a429e32a to your computer and use it in GitHub Desktop.
Move item in an array JS
const moveInArray = (arr, from, to) => {
if (to >= arr.length) {
var k = to - arr.length + 1
while (k--) {
arr.push(undefined)
}
}
arr.splice(to, 0, arr.splice(from, 1)[0])
return arr
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment