Skip to content

Instantly share code, notes, and snippets.

@hungtrn75
Created August 1, 2019 03:34
Show Gist options
  • Save hungtrn75/36ad3918735c220f8e43baedea871ecb to your computer and use it in GitHub Desktop.
Save hungtrn75/36ad3918735c220f8e43baedea871ecb to your computer and use it in GitHub Desktop.
function immutableMove(arr, from, to) {
return arr.reduce((prev, current, idx, self) => {
if (from === to) {
prev.push(current);
}
if (idx === from) {
return prev;
}
if (from < to) {
prev.push(current);
}
if (idx === to) {
prev.push(self[from]);
}
if (from > to) {
prev.push(current);
}
return prev;
}, []);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment