Skip to content

Instantly share code, notes, and snippets.

@duggiemitchell
Created July 18, 2023 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duggiemitchell/a7c7ff8a75b071e8c7c329b9629b8709 to your computer and use it in GitHub Desktop.
Save duggiemitchell/a7c7ff8a75b071e8c7c329b9629b8709 to your computer and use it in GitHub Desktop.
immutably remove an item from a list
// 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