Created
July 18, 2023 17:15
-
-
Save duggiemitchell/a7c7ff8a75b071e8c7c329b9629b8709 to your computer and use it in GitHub Desktop.
immutably remove an item from a list
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
// source list | |
const theList = [{id: 'foo'}, {id: 'bar'},{id: 'bazzle'} ]; | |
// the item to remove | |
const item = {id: 'foo'}; | |
// get the item's index from soure list | |
const indx = theList.findIndex(v => v.id === item.id) | |
// function to remove | |
const removeItemAtIndex = (arr, index) => { | |
return [...arr.slice(0, index), ...arr.slice(index + 1)] | |
} | |
// usage | |
const newList = removeItemAtIndex(selectedMembers, indx) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment