Array Destructuring
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercisesExercise 1 — Unpacking an Array
Imagine you have a team of three Codémon and you want each one in its own variable. The obvious way is to read them one index at a time:
const team = ["Aquachu", "Pyrochu", "Florachu"];
const first = team[0];
const second = team[1];
const third = team[2];
console.log(first + " / " + second + " / " + third);It works, but it's three lines that all say the same thing. Destructuring does it in one: you write the variable names inside brackets, on the left of the `=`.
const team = ["Aquachu", "Pyrochu", "Florachu"];
const [first, second, third] = team;
console.log(first + " / " + second + " / " + third);Exercise 2 — Position Decides Everything
The names you choose don't matter, only their position. The first name gets index 0, the second gets index 1, and so on:
const [a, b, c] = array;
// a = array[0], b = array[1], c = array[2]If you ask for more names than the array has elements, the extra ones are `undefined`. No error, exactly like reading an index too far:
const team = ["Aquachu", "Pyrochu"];
const [first, second, third] = team;
console.log(third);Exercise 3 — Quiz: Who Gets What?
What does this code print?
const stats = [40, 12, 30];
const [hp, attack, speed] = stats;
console.log(attack);124030undefinedHint : Match each name with its position in the array.
Exercise 4 — Unpack the Team
Put the first two Codémon into `leader` and `second`.
Hint : Destructuring an array uses the same kind of brackets as the array itself.
Exercise 5 — Skipping Elements
Sometimes you only want certain elements from an array. To skip an element, leave an empty slot by writing a comma with no variable name:
const team = ["Aquachu", "Pyrochu", "Florachu"];
// Skip Pyrochu (index 1)
const [first, , third] = team;
console.log(first + " and " + third);And if you only need the beginning of the array, you don't need extra commas at the end: any remaining elements are simply ignored.
const team = ["Aquachu", "Pyrochu", "Zap", "Rockachu"];
const [leader] = team;
console.log(leader);Exercise 6 — Quiz: The Empty Slot
What does this code print?
const moves = ["Spark", "Splash", "Thunder"];
const [, second] = moves;
console.log(second);SplashSparkThunderundefinedHint : The empty slot still takes a position.
Exercise 7 — Swapping in One Line
Swapping two variables used to need a temporary one. With destructuring it's a single line: build an array with the two values, then unpack it back in the other order.
let leader = "Aquachu";
let backup = "Zap";
[leader, backup] = [backup, leader];
console.log(leader + " leads, " + backup + " waits");let, not const
The two variables are reassigned, so they must be declared with `let`. With `const` this line throws an error.
Exercise 8 — Skip the Middle
Take the first and the third level, ignoring the one in between.
Hint : An ignored position still needs its separator, and so does the next name.
Exercise 9 — Array Destructuring Cheat Sheet
`const arr = [1, 2, 3];`. Match each line with the value it produces.
const [a] = arrconst [, b] = arrconst [, , c] = arrconst [a, b, c, d] = arrExercise 10 — Podium
The tournament results are stored in order, from the winner to the last place. Turn them into a readable podium.
Step 1: with a single line of destructuring, put the first three results into `gold`, `silver` and `bronze`. Step 2: print one line per medal, in the format shown below. Step 3: still with destructuring, put the first and the third result into `champion` and `third`, ignoring the second, then print `"champion beat third"`.
Expected output:
Gold: Metachu
Silver: Aquachu
Bronze: Zap
Metachu beat Zapconst results = ["Metachu", "Aquachu", "Zap", "Pyrochu"];
// Write your code here