Loading video player…

Filtering and Transforming With filter() and map()

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"] is runners.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’s filter(), 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] is runners.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"] becomes runners.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".finish is undefined, and undefined !== "DNF" is true. In Python, filtering the names by their finish stops with TypeError: 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. runners is untouched. Python’s own filter() and map() 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 a for...of loop that pushes onto a new array.

Here are the finishers’ names, with the seven records written out:

Language: JavaScript
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:

Language: Program Output
[
  '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.

00:00 In the welcome video, a one-line port listed Lars Berg with the finishers, though he didn’t finish. To see why, you’ll port the comprehension’s two halves first.

00:11 Each of the desk’s seven runners is a record with a name and a finish. Lars Berg’s is DNF. Python keeps the finishers with a comprehension and a condition.

00:23 It prints six. JavaScript has no comprehensions. Instead, arrays have a filter() method: Python’s filter(), called on the array itself.

00:36 It runs an arrow function once per runner. The arrow returns its comparison, and filter() keeps each runner it returns true for, in a new array.

00:46 JavaScript logs six.

00:50 A comprehension without a condition transforms every item. Here, Python takes each runner’s name, and prints seven of them. In JavaScript, the map() method does this job.

01:04 It runs the arrow once per runner and collects each return value into a new array. Seven runners go in, and seven names come out. JavaScript logs the same seven, Lars Berg included.

01:19 The results page needs only the finishers’ names. Python does both jobs in one comprehension. It reads name first, then the loop, then the condition. It prints six names.

01:34 Ported in the order it reads, map() takes each name, then filter() keeps the finishers. Each method is called on the array the one before it returns, so the two calls chain.

01:46 This is the port from the welcome video. Run it, and JavaScript logs seven names. Lars Berg is back, and there’s no error.

01:57 Look at what filter() gets now. It’s called on the seven names that map() returned, so its arrow receives a string, not a runner.

02:06 For the string Lars Berg, what does r.finish give? Your turn: pause the video and predict.

02:18 It gives undefined, with no error.

02:23 A string has no finish property, and reading a property that isn’t there gives undefined in JavaScript. undefined isn’t DNF, so the test is true, and filter() keeps every name.

02:37 Make the same slip in Python, filtering the names by their finish, and it stops with a type error. A comprehension can’t make it, because it filters the original runners first, then transforms the ones it keeps.

02:51 A chain runs left to right, in the order you write it.

02:57 So swap the two steps. filter() tests each runner’s finish, and map() takes the names of the six it keeps. Now the chain reads in the order it runs.

03:08 Run it, and JavaScript logs six names, the same as Python. Each step runs straight away and returns a new array: seven runners, then six finishers, then six names.

03:22 The runners array is untouched, and still holds seven. Python’s own filter() and map() are lazy instead: they hand back iterators that work only when you loop over them.

03:34 Lazy pipelines come up later.

03:38 Where Python reaches for a comprehension, JavaScript reaches for a chain. Filter first, then map, so the chain reads in the order it runs. With no condition, it’s one map() call.

03:52 And like a crowded comprehension that becomes a loop with append(), a chain that needs comments becomes a for...of loop that pushes onto a new array.

04:01 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.

Become a Member to join the conversation.