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

Follow Data Through a Pipeline

In this lesson, you followed Marco’s teams through two chains of array methods, the final standings and the night’s perfect tens, and said what the data looked like after every step.

Key Points

  • Each method in a chain is called on the array the step before it returned. Chains are usually written one step per line. To read a chain, say what each step hands on: how many items, and what each one looks like. That’s what the next step receives.
  • In the standings chain, the first .map() builds a new record for each team, and .reduce() inside it adds the team’s five rounds into one number. That step hands on five records, each with only a name and a total.
  • .toSorted() puts those records in order, highest total first. It still hands on five records of the same shape, and only the order changes. The last .map() turns each record into a line of text, so the chain ends with five strings.
  • Watch out: a chain that starts by calling .sort() on teams reorders Marco’s own array, because .sort() works in place. .toSorted() always sorts a copy of the array, wherever it sits in a chain. The copy holds the same records.
  • Mapping each team to its rounds gives five items, and each item is an array of five scores, so you have an array of arrays. Watch out: a .filter() for 10 on that array compares whole arrays with 10, so it keeps none and gives [].
  • .flat() joins the inner arrays into one array of 25 numbers. .flatMap() does both steps at once: it maps each team to its rounds, then flattens the result.
  • A long chain keeps its middle arrays out of sight. When Kwame doubted the Know-It-Owls’ 30, you named the first step’s array totals and logged it. The log showed their total was already 30 before the sort, because of their 0 in round three. Name a middle array you want to read, and leave a step you never need to see in the chain.

Final Code

Here’s standings.js, with the final standings as one chain:

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 Marco’s scorers keep each team’s total during the night. At the end, he wants the final standings: every team with its total, best first. Here are tonight’s five teams.

00:11 Each one has a name, a captain, and its points for five rounds.

00:17 No single array method does all of that, but a chain can. Each method in a chain is called on the array the step before it returned. Chains are usually written one step per line.

00:28 In this video, you’ll follow the data through longer chains, and say what it looks like after every step.

00:36 Here’s the chain in Marco’s standings file. It starts with teams: five team records. The first .map() builds a new record for each team.

00:45 Inside it, .reduce() adds the team’s five rounds into one number. So this step hands on five records, each with only a name and a total. Next, .toSorted() puts those records in order, highest total first.

01:00 It’s still five records of the same shape. Only the order changed. Watch out: a chain that starts by calling .sort() on teams reorders Marco’s own array, because .sort() works in place.

01:13 .toSorted() always sorts a copy of the array, wherever it sits in a chain. The last .map() turns each record into a line of text, so it hands back five strings.

01:24 Run it, and the Lamplighters top the standings on 40, with the Know-It-Owls last, on 30.

01:32 Marco’s next question: how many perfect tens did the teams score tonight? For that, you need every round score in one array.

01:41 A map from each team to its rounds gives you five items, and each item is an array of five scores. That’s an array of arrays. Watch out: if you filter that for tens, each item is a whole array, never the number ten, so the filter keeps none.

02:00 The .flat() method joins the inner arrays into one array of twenty-five numbers. And .flatMap() does both steps at once: it maps each team to its rounds, then flattens the result.

02:12 Then .filter() keeps the scores that equal ten, and tens holds them. Your turn: pause the video and predict how many items tens holds.

02:27 It holds two. The Lamplighters scored ten in the last round, and the Quizzards in the second.

02:35 After the quiz, Kwame, the Know-It-Owls’ captain, says thirty can’t be right. Was the total wrong, or did the sort mix up the teams? The chain keeps its middle arrays out of sight, so split it into named steps.

02:50 Here’s the standings chain again. Now totals holds the first step’s records, and standings chains the sort and the lines onto totals.

02:59 The result is the same. Log totals, and you see every total before the sort. The Know-It-Owls already have 30 before the sort, because of their zero in round three.

03:10 Name a middle array you want to read. A step you never need to see can stay in the chain. So read a chain one step at a time. After each step, say what it hands on: how many items, and what each one looks like.

03:26 That’s exactly what the next step receives.

03:30 Next, in Wrap Functions to Add Behavior, you’ll wrap Marco’s scoring functions in new ones that add logging and checks.

Become a Member to join the conversation.