Method Chaining
The full text of this chapter, to read or revisit. To practise, do the exercises.
βΆ Do the exercisesExercise 1 β One Result Feeds the Next
A real task is rarely a single step. Here you have your team's levels. You only care about the high ones, and then you want to display them as labels like `Lv.12`. That's two operations one after the other: first select, then transform.
const levels = [4, 12, 9, 15];
const high = levels.filter((level) => level >= 10);
const labels = high.map((level) => "Lv." + level);
console.log(labels);The `high` variable is only used on the next line. You can skip it and call `map` directly on what `filter` returned. That's chaining:
const levels = [4, 12, 9, 15];
const labels = levels.filter((level) => level >= 10).map((level) => "Lv." + level);
console.log(labels);Because each of these methods returns an array, the result of the first can go straight into the second. It's not magic, it's code :)
Exercise 2 β One Method per Line
A chain quickly becomes hard to read on a single line. To help readability, each method is put on its own line, starting with the dot:
const team = [
{ name: "Aquachu", hp: 40 },
{ name: "Pyrochu", hp: 0 },
{ name: "Zap", hp: 25 }
];
const readyNames = team
.filter((cdm) => cdm.hp > 0)
.map((cdm) => cdm.name);
console.log(readyNames);JavaScript doesn't care about the line breaks. Each line reads as one step: filter the ready ones, then take their names.
Exercise 3 β Quiz: Chain Result
What does `result` contain?
const nums = [1, 2, 3, 4];
const result = nums
.filter((n) => n % 2 === 0)
.map((n) => n * 10);[20, 40][10, 20, 30, 40][2, 4][10, 30]Hint : Apply the first step to the whole array, then the second step to what's left.
Exercise 4 β Ready Names
Keep the members with HP above 0, then extract their names.
Hint : First select, then transform.
Exercise 5 β Order Matters
Each step works on the output of the previous one, so the order changes the result. Here a bonus of 5 is added first, then the levels of 10 or more are kept:
const levels = [4, 12, 9];
const result = levels
.map((level) => level + 5)
.filter((level) => level >= 10);
console.log(result);Now the same two steps, swapped: the levels are selected first, and only the survivors get the bonus.
const levels = [4, 12, 9];
const result = levels
.filter((level) => level >= 10)
.map((level) => level + 5);
console.log(result);In the first version, the bonus is added before the test, so 9 becomes 14 and passes. In the second, 9 is dropped before getting the bonus. Always think first about which step should see which data.
Exercise 6 β Quiz: Swapped Steps
What does `result` contain?
const nums = [3, 8];
const result = nums
.map((n) => n * 2)
.filter((n) => n > 10);[16][6, 16][8][]Hint : Double first, then keep only what's above 10.
Exercise 7 β Ending a Chain
A chain can end with something that isn't an array: `length` to count, `find` to pick one, or `forEach` to act. Nothing can be chained after those.
const team = [
{ name: "Aquachu", hp: 40 },
{ name: "Pyrochu", hp: 0 },
{ name: "Zap", hp: 25 }
];
const readyCount = team.filter((cdm) => cdm.hp > 0).length;
console.log("Ready: " + readyCount);const team = [
{ name: "Aquachu", hp: 40 },
{ name: "Pyrochu", hp: 0 },
{ name: "Zap", hp: 25 }
];
team
.filter((cdm) => cdm.hp > 0)
.map((cdm) => cdm.name + " is ready")
.forEach((line) => console.log(line));Exercise 8 β Count the Veterans
Count how many levels are 10 or more, in a single chain.
Hint : Select first, then read the size of what's left.
Exercise 9 β Chaining Cheat Sheet
Match each element with its description.
a.filter(f).map(g)a.map(g).filter(f)a.filter(f).lengtha.map(g).forEach(h)Exercise 10 β Battle Report
Produce a battle report using chains only: no loops, no intermediate variables except the ones named below.
Step 1: `readyLines` is an array of strings `"name (X HP)"` for every member with HP above 0. Print each line. Step 2: print `"Fainted: N"` with the number of members at 0 HP, computed in one chain. Step 3: print `"Strongest ready: name"` with the first ready member whose level is 10 or more.
Expected output:
Aquachu (40 HP)
Zap (25 HP)
Metachu (60 HP)
Fainted: 1
Strongest ready: Aquachuconst team = [
{ name: "Aquachu", hp: 40, level: 12 },
{ name: "Pyrochu", hp: 0, level: 8 },
{ name: "Zap", hp: 25, level: 4 },
{ name: "Metachu", hp: 60, level: 15 }
];
// Write your code here