Menu
CoddyTech

Counts from Lists

Part of the Fundamentals section of Coddy's Vue journey. Lesson 29 of 55.

A summary can be derived from record fields. This computed ref counts unread books and changes when a book's read field changes.

const unread = computed(() => books.value.filter(book => !book.read).length);

Keep one source of truth in the records. A separately incremented counter can become incorrect when items are added, removed or toggled. Deriving the count handles these paths and gives zero for an empty list. A complementary count can use total length minus the first count.

challenge icon

Challenge

Medium

Use a computed value to count unfinished records in the supplied array. Display 1 remaining initially in #result. Render a button per record with id toggle- plus its id. Each button toggles that record's done field and the count must follow.

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