Skip to content

Instantly share code, notes, and snippets.

@kevinsalter
Last active June 25, 2023 12:04
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kevinsalter/7a4a3cf64a6783ec755f697f93693f82 to your computer and use it in GitHub Desktop.
Save kevinsalter/7a4a3cf64a6783ec755f697f93693f82 to your computer and use it in GitHub Desktop.
reordering array using ES2015 array spread operator
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;
}
@andrebraga7
Copy link

The code is really elegant, well done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment