Learning objective: Build a collection-based application: keep state in an array of objects, write functions that add, update, remove and summarise items, and test each operation.
Skills practised: Arrays, Working with collections, Application design.
04Explanation
Many applications are a collection (an array of objects) plus operations on it: add, find, update, remove, list and count.
Give each item a unique id so you can find it reliably, even when titles repeat or the order changes.
Write one function per operation: addTask, completeTask(id), removeTask(id), summary(). Each function does one job and can be tested on its own.
Keep display separate from data. A render() or printList() function reads the array and prints it; the other functions only change data.
Generate ids from a counter (nextId++) rather than length + 1, which repeats ids after a removal.
Two common styles: mutate a module-private array through a small API, or treat state as immutable and return a new array from every operation ([...tasks, task], tasks.map(...)). The immutable style makes undo, history and change detection simple.
Validate inputs at the boundary (empty titles, unknown ids) and return clear results or throw meaningful errors, instead of silently doing nothing.
Test operations in sequence: add, update, remove, then check the summary. Sequences catch bugs, such as reused ids, that single calls do not.
05Definitions
Collection
A group of similar items, usually stored as an array of objects.
State
The data an application holds at a moment in time.
Unique id
A value that identifies exactly one item in a collection.
CRUD
Create, Read, Update, Delete: the basic operations on a collection.
const tasks = [
{ id: 1, title: "Plan", done: false },
{ id: 2, title: "Build", done: true }
];
function toggleTask(id) {
// TODO: flip done for the task with this id and return the new value, or null if not found
}
console.log(toggleTask(1), toggleTask(2), toggleTask(5));
Reading list application
Build add, finish, remove and summary operations on one collection.
const books = [];
let nextId = 1;
// Write addBook(title, pages), finishBook(id), removeBook(id) and stats()
// stats() returns { count, finished, pagesRead } where pagesRead counts finished books only
09Prediction
Commit to an answer first, then run the real code to compare.
A community event keeps attendees in an array of objects. Organisers add people, mark check-ins by id, remove cancellations and see live counts, all through small, tested functions.
Data logic can be tested without output, and display can change independently
It is faster
It uses less code
6. Which id strategy is safest?
length + 1
A counter that only increases
The item title
Math.random()
13Solutions and tests
Each exercise's solution appears in the exercise once you have run its tests. Solutions are one possible answer: the tests accept any implementation with the same behaviour.
Automated acceptance tests for this lesson
These are the same checks the exercises run. They are visible on purpose: client-side tests help you learn and are not secret.
Unique ids: Ids stay unique; Uses a counter
Toggle a task: Prints the results; State updated
Reading list application: Prints the stats; Unknown id is rejected; stats returns an object
ex-030-01: output matches the documented expected output
ex-030-02: output matches the documented expected output
ex-030-03: output matches the documented expected output
ex-030-04: output matches the documented expected output
The lesson lives in the companion repository at curriculum/level-02-core-development/module-06-arrays-and-collections/lesson-030-building-collection-based-applications/. Every example on this page is generated from those files.
The GitHub repository (https://github.com/lsvekis/JavaScript-Professional-Academy) has not been published yet, so these links will work once it is. Until then, use the ZIP download above.
15Summary and next steps
Applications often manage an array of objects.
Give items unique ids from a counter.
One small function per operation; check for not-found cases.
Copy a prompt into the AI assistant of your choice. Then verify what it says by running code here. No AI service is connected to this page.
Concept explanationcollection-based applications
Context: I am learning collection-based applications in JavaScript.
Task: Show me how to design a small to-do application around one array of objects with add, complete, remove and summary functions.
Constraints: Beginner-friendly language, short runnable examples, no libraries.
Expected behaviour: I can explain the idea in my own words and predict small programs.
Testing: Finish with three quick questions for me to answer.
Context: I have practised collection-based applications.
Task: Give me five sequences of collection operations and ask what the final state is.
Constraints: Wait for my answer before revealing each result.
Expected behaviour: Each question has exactly one correct output.
Testing: Explain any I miss with a step-by-step trace.
Debuggingcollection-based applications bug
Context: Removing an item from my list sometimes removes the wrong one.
Task: Help me find the cause with questions instead of giving me the fix.
Constraints: Do not rewrite my program.
Expected behaviour: I locate and fix the bug myself.
Testing: Suggest the console.log lines that would reveal the problem.
Exercise generationcollection-based applications practice
Context: I finished the collection-based applications exercises.
Task: Specify two small collection applications (contacts, inventory) with operations and expected output.
Constraints: Fictional data and exact expected output.
Expected behaviour: Each exercise has tests I can run.
Testing: Put the solutions in a separate hidden section.
Code reviewcollection-based applications review
Context: Here is my code that uses collection-based applications.
Task: Review my collection functions for id handling, not-found cases and separation of display from data.
Constraints: Explain each suggestion briefly and keep behaviour identical.
Expected behaviour: The code is clearer and has no hidden mutation or edge-case bug.
Testing: List the changes in order of importance.