Filtering and Transforming With filter() and map()
In this lesson, you ported the results script’s list comprehensions to JavaScript. JavaScript has no comprehensions: arrays have methods that take a function, and they do the job.
- A comprehension with a condition becomes
filter(). Python’s[r for r in runners if r["finish"] != "DNF"]isrunners.filter((r) => r.finish !== "DNF"), which runs the arrow once per runner and keeps each runner the arrow returns a truthy value for (here,true): six of the seven. Like Python’sfilter(), but called on the array itself, and it hands back a new array. - A comprehension with no condition becomes one
map(). Python’s[r["name"] for r in runners]isrunners.map((r) => r.name), which collects each return value into a new array: seven runners in, seven names out. - Ported in the order it reads,
[r["name"] for r in runners if r["finish"] != "DNF"]becomesrunners.map((r) => r.name).filter((r) => r.finish !== "DNF"), and JavaScript logs all seven names, Lars Berg included, with no error. The filter now gets strings:"Lars Berg".finishisundefined, andundefined !== "DNF"istrue. In Python, filtering the names by their finish stops withTypeError: string indices must be integers, not 'str'. - A comprehension filters the original items first, then transforms the ones it keeps. A chain runs left to right, in the order you write it. So filter first, then map, and the chain reads in the order it runs.
- Each step runs straight away and returns a new array: 7 runners, 6 finishers, 6 names.
runnersis untouched. Python’s ownfilter()andmap()are lazy: they return iterators that do the work only when you loop over them. - Like a crowded comprehension that becomes a loop with
append(), a chain that grows long enough to need comments becomes afor...ofloop that pushes onto a new array.
Here are the finishers’ names, with the seven records written out:
const runners = [
{
bib: 7,
name: "Ana Ruiz",
group: "F35",
checkpoints: ["0:24:10", "0:48:35", "1:13:02"],
finish: "1:42:37",
},
{
bib: 12,
name: "Tom Okafor",
group: "M40",
checkpoints: ["0:21:05", "0:42:30", "1:04:12"],
finish: "1:30:48",
},
{
bib: 23,
name: "Mei Chen",
group: "F35",
checkpoints: ["0:22:40", "", "1:08:55"],
finish: "1:36:20",
},
{
bib: 31,
name: "Lars Berg",
group: "M40",
checkpoints: ["0:23:15", "0:47:01", ""],
finish: "DNF",
},
{
bib: 44,
name: "Priya Nair",
group: "F20",
checkpoints: ["0:20:30", "0:41:10", "1:02:05"],
finish: "1:27:44",
},
{
bib: 58,
name: "Sam Kowalski",
group: "M40",
checkpoints: ["0:22:50", "0:45:12", ""],
finish: "1:35:03",
},
{
bib: 66,
name: "Jo Lindqvist",
group: "F20",
checkpoints: ["0:23:30", "0:47:55", "1:12:40"],
finish: "1:41:15",
},
];
const names = runners
.filter((r) => r.finish !== "DNF")
.map((r) => r.name);
console.log(names);
It logs the six finishers, as Python’s comprehension does:
[
'Ana Ruiz',
'Tom Okafor',
'Mei Chen',
'Priya Nair',
'Sam Kowalski',
'Jo Lindqvist'
]
Next up is the text lesson Passing Functions to map(). There, you’ll turn each finish time into
seconds, and see what map() passes the function you give it.