Menu
CoddyTech

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 icon

Challenge

Medium

Render 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>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals