Created
November 25, 2018 18:04
-
-
Save AyoAlfonso/ce9837415a25f4f45c1f76ad5e94a984 to your computer and use it in GitHub Desktop.
Remove Elements Without Filtering
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
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