Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Transform and Filter Arrays

In this lesson, you turned Tomás’s watchlist into labels for the group chat and picked out the animated films, by handing a callback to array methods instead of writing a loop.

Key Points

  • A loop that builds a new array always has an empty array, the loop itself, and a push() call. Only the expression that builds each new item changes. With map() and filter(), you hand in that part as a callback.
  • map() calls your callback once for each item, in order, and passes the item in. Each return value goes into a new array at the same position, so the result always has the same length as the original.
  • filter() also calls your callback once for each item, but it doesn’t collect the return values. It keeps the item itself when the callback returns a truthy value and leaves it out otherwise. A count works as a test, because 0 is falsy.
  • The callback’s return decides the result. Your isShort helper takes minutes, but map() and filter() hand the callback the whole film, so fitsTonight takes a film. Hand fitsTonight to both, and map() gives back six true or false values, while filter() gives back the four films that fit.
  • Neither method changes the original array. Each one builds a new array and returns it. The films that filter() returns are the watchlist’s own records, so a change to one would show in both.
  • If you give an arrow a braces body, it needs a return statement, or every value map() collects is undefined.

Final Code

This script starts with the watchlist array from helpers.js in the Store Functions in Variables lesson, then transforms and filters it:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Tomás’s watchlist now holds full film records, and it’s up to date. In this lesson, you’ll hand a function to methods every array has, and watch them call it once for every film.

00:12 Tomás wants a label for each film to post in the group chat: the title, with its running time in parentheses. With what you already know, you’d write a loop.

00:21 It starts with an empty array, goes through the films, and pushes one label for each. Run it, and you get six labels for six films. Loops that transform a list all look like this.

00:33 The empty array, the loop and the push stay the same. Only the expression that builds each label changes. Earlier in this course, you handed countdown the part that changes, as a function.

00:45 Arrays let you do the same.

00:48 The map method takes a callback. Here, it’s an arrow that takes one film and returns its label, the same expression as before.

00:57 Map calls your arrow once for each film, in order, and passes the film in. Whatever the arrow returns goes into a new array, at the same position. Paddington 2 goes in, and its label comes out.

01:10 Then Spirited Away, and so on, down to Inception. Run it, and labels holds the same six strings as the loop’s array. Map always gives back a new array with the same length as the original, one return value per item.

01:25 Watch out: if you wrap the arrow’s body in braces, it needs a return statement, or every label comes back as undefined, one for each film.

01:37 Aisha wants an animated film tonight. The filter method also calls your callback once per film. This time, the arrow returns true or false, from a question: is this film’s genre animation?

01:50 Filter doesn’t collect what the arrow returns. When the return is true, it puts the film itself in the new array. It’s the watchlist’s own record, so a change to one would show in both.

02:01 That’s all you need about that for now. When it’s false, the film is left out. Run it, and only Spirited Away and Coco make it through. The return doesn’t have to be exactly true.

02:14 Filter keeps an item for any truthy value, so the length of a film’s seenBy list works as a test. Zero is falsy, so this filter keeps five films and drops only Arrival, which nobody has seen.

02:29 Your isShort helper takes minutes, but map and filter hand over the whole film. So fitsTonight takes a film, and returns true when it runs 120 minutes or less, Tomás’s limit.

02:41 Pass it to map without parentheses, so map can call it. Your turn: pause the video and predict what this line logs.

02:54 It logs six true or false values, one per film. Map keeps every return value, whatever it is. Hand the same function to filter, and you get four films, the ones whose return was true.

03:09 So the callback’s return decides the result. Map keeps every return value. Filter keeps every item whose return is truthy. And both leave the original array alone, so its length is still six.

03:23 Six films make a toy watchlist, but these methods work the same on any list. Next, in Chain Methods, Use forEach, or Loop?, you’ll read when to combine filter and map, and when a plain loop is the better choice.

Become a Member to join the conversation.