Skip to content

Instantly share code, notes, and snippets.

@sarfarazansari
Created September 13, 2018 11:59
Show Gist options
  • Save sarfarazansari/d9a087ec3dc1dec051b6767497ba9e5e to your computer and use it in GitHub Desktop.
Save sarfarazansari/d9a087ec3dc1dec051b6767497ba9e5e to your computer and use it in GitHub Desktop.
move array position by passing old and new positions.
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