Created
April 27, 2021 15:42
-
-
Save autr/143d839163e637b25c703dd2a429e32a to your computer and use it in GitHub Desktop.
Move item in an array JS
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
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