Last active
June 25, 2023 12:04
-
-
Save kevinsalter/7a4a3cf64a6783ec755f697f93693f82 to your computer and use it in GitHub Desktop.
reordering array using ES2015 array spread operator
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 reorderArray = (event, originalArray) => { | |
const movedItem = originalArray.find((item, index) => index === event.oldIndex); | |
const remainingItems = originalArray.filter((item, index) => index !== event.oldIndex); | |
const reorderedItems = [ | |
...remainingItems.slice(0, event.newIndex), | |
movedItem, | |
...remainingItems.slice(event.newIndex) | |
]; | |
return reorderedItems; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The code is really elegant, well done.