Created
August 1, 2019 03:34
-
-
Save hungtrn75/36ad3918735c220f8e43baedea871ecb to your computer and use it in GitHub Desktop.
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
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