Map, Filter and Reduce
Ask three different questions about one marks list
A teacher has marks 40, 20 and 35. The report needs a bonus-adjusted list, a list of passing marks and a total. Each task uses the same collection but asks a different question.
JavaScript arrays provide map to transform each item, filter to select items and reduce to combine items into a result. Choosing by the question makes the code easier to explain.
Choose an operation
Distinguish transformation, selection and accumulation.
Trace the callback
Follow values through one array operation.
Handle an empty list
Supply a meaningful initial value when reducing.
Different questions produce different result shapes
Transform every mark
Add two to each mark.
42, 22, 37Select passing marks
Keep marks at least 25.
40, 35Combine the marks
Start from zero and add each mark.
95
The callback describes what happens to one item
map calls its callback for each existing array entry and collects the returned values into a new array. filter keeps an entry when the callback result is truthy. Neither method changes this original array by itself.
reduce passes an accumulator and the current item to its callback. The returned value becomes the next accumulator. With initial value zero, our totals progress 0 → 40 → 60 → 95.
Before starting, review passing functions as values. These examples use JavaScript; other languages can use different syntax and rules.
Build three report results from the marks
The arrow callbacks name one mark at a time. mark + 2 transforms; mark >= 25 tests inclusion; total + mark combines. join displays each array as comma-separated text.
The original marks are also displayed, showing that these numeric callbacks left the source array unchanged.
JavaScript · local teaching example
const marks = [40, 20, 35];
console.log(marks.map(mark => mark + 2).join(","));
console.log(marks.filter(mark => mark >= 25).join(","));
console.log(marks.reduce((total, mark) => total + mark, 0));
console.log(marks.join(","));Expected output
42,22,37
40,35
95
40,20,35Give an empty total a defined starting point
An empty marks list has total zero under this rule. Supplying the initial value lets reduce return zero without calling the callback.
Without an initial value, reduce uses the first entry when one exists and throws for an empty array. A starting value is therefore part of the calculation, not decoration.
JavaScript · local teaching example
const marks = [];
console.log(marks.reduce((total, mark) => total + mark, 0));Expected output
0Apply the rule to a small change
Keep only passing marks from 10, 25 and 40, then total those passing marks.
Worked answer
filter keeps the boundary mark 25 and mark 40. reduce starts from zero and adds them, so the passing total is 65. The failed mark 10 does not contribute.
JavaScript · local teaching example
const marks = [10, 25, 40];
const passing = marks.filter(mark => mark >= 25);
const total = passing.reduce((sum, mark) => sum + mark, 0);
console.log(passing.join(","));
console.log(total);Expected output
25,40
65Recognize mistakes and limits
Creating a new array does not guarantee that nested objects remain unchanged. A callback that changes student.mark mutates that shared object; use deliberate copies when changes should be isolated.
Return the result from a callback body with braces. mark => { mark + 2; } returns undefined because there is no return statement.
A clear loop can be better for complicated branching or several state updates. Do not force every task into a long chain just because these methods are available.
For the language or tool rules used here, see JavaScript array callbacks.
Check the core idea
Apply the idea to a new situation
Match the operation to the report question
Use map for a transformed list, filter for a selected list and reduce for an accumulated result. Explain the callback and the starting value before combining operations.
Connect this idea with the equivalent loop mechanism and callbacks that leave inputs unchanged.
Reaching the recap and completing the quizzes are separate milestones.