Created
September 13, 2018 11:59
-
-
Save sarfarazansari/d9a087ec3dc1dec051b6767497ba9e5e to your computer and use it in GitHub Desktop.
move array position by passing old and new positions.
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
public moveArrayPosition(arr: any[], oldPosition: number, newPosition: number) { | |
if (newPosition >= arr.length) { | |
let k = newPosition - arr.length + 1; | |
while (k--) { | |
arr.push(undefined); | |
} | |
} | |
arr.splice(newPosition, 0, arr.splice(oldPosition, 1)[0]); | |
return arr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment