Removing by Id
Part of the Fundamentals section of Coddy's Vue journey. Lesson 27 of 55.
Filter creates a new array containing items to keep. This handler removes the requested book id while keeping other records, including books that happen to share a title.
function removeBook(id) {
books.value = books.value.filter(book => book.id !== id);
}Assign the returned array back to the ref. Calling filter without using its result does not change the source. Pass the identity from the clicked row instead of relying on its current position, because sorting or filtering can change positions.
Challenge
MediumRender the supplied supplies as keyed rows in ul#result. Each row displays its name and a button with id remove- plus the item id. Clicking removes only that id. Removing all rows must leave an empty list.
Try it yourself
<!doctype html><html><head><meta charset="UTF-8"><title>Vue practice</title></head><body><div id="app"></div><script type="module" src="./main.js"></script></body></html>This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
2Reactive State
Reactive RefsUpdating Refs in ScriptEvent Handler ArgumentsReactive ObjectsRecap: Reactive State5Conditions and Lists
Conditional BranchesShow without UnmountingRendering ListsStable List IdentityRecap: Conditions8Component Communication
Emitting EventsEvent PayloadsLifting Shared StateDefault and Named SlotsRecap: Shared Panel11Project: Community Board
Board StructureAdding Community Tasks6Working with Lists
Adding List ItemsRemoving by IdComputed Search ResultsCounts from ListsRecap: List Manager