filter, map & reduce
Build a small transformation pipeline
Lesson 15 of 20 · 12 min · Module 5: Reliable data pipelines
A pipeline gives each stage one job. Select the records you want, transform their values, then combine those values into a result.
Start this lesson →Free. No installation: you write and run the code in your browser, and it is checked automatically.
By the end, you’ll be able to…
- Give each collection operation a clear role
- Choose an initial accumulator
The idea
filter keeps matching items, map produces a value per item, and reduce accumulates a result. An explicit initial accumulator makes an empty collection predictable.
const total = [2, 4, 6].reduce((sum, value) => sum + value, 0);
console.log(total);
01Name the question at each stage
First ask which records count. Then ask which value you need from each. Finally decide how to combine those values.
02Handle the empty path
No completed sessions means filter returns an empty array. reduce with the initial value zero can still return a valid result.
console.log([].reduce((sum, n) => sum + n, 0));003Prefer readable steps
A single reduce can also solve this exercise. Choose the version whose intermediate meaning is clearest, rather than assuming a longer chain is always better.
Your turn
completedMinutes(sessions). Include only sessions whose completed property is exactly true. Convert their minutes to a total without changing the records. The provided minutes are numbers.What the checks look for
- Totals only completed records
- Handles an empty collection
- Requires a real boolean true
- Does not modify the supplied records
Take this with you
A pipeline should make the intermediate questions easy to explain.
Go a little further
Return both total minutes and the titles of completed sessions from one report function.