Last active
May 16, 2022 12:40
-
-
Save SebastianUdden/8c9d270611530bf8420c9894c86f3e52 to your computer and use it in GitHub Desktop.
Swap element positions in an array
This file contains hidden or 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 handleChangePosition = ( | |
| oldIndex, | |
| newIndex, | |
| array | |
| ) => { | |
| if (newIndex < 0 || newIndex > array.length - 1) return []; | |
| const newArray = [...array]; | |
| const oldObj = array[oldIndex]; | |
| const newObj = array[newIndex]; | |
| newArray[oldIndex] = newObj; | |
| newArray[newIndex] = oldObj; | |
| return newArray; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment