Skip to content

Instantly share code, notes, and snippets.

@AyoAlfonso
Created November 25, 2018 18:04
Show Gist options
  • Save AyoAlfonso/ce9837415a25f4f45c1f76ad5e94a984 to your computer and use it in GitHub Desktop.
Save AyoAlfonso/ce9837415a25f4f45c1f76ad5e94a984 to your computer and use it in GitHub Desktop.
Remove Elements Without Filtering
export const removeElement = (list, id) => {
const removeIndex = list.findIndex(item => item.id === id)
if (removeIndex == -1) {
return list;
/*Return the original array if you cant find the id to delete*/
}
if (removeIndex == 0){
return ...list.slice(1);
}
return [
...list.slice(0,removeIndex),
...list.slice(removeIndex+1)
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment