Accessing Nested Data

The full text of this chapter, to read or revisit. To practise, do the exercises.

▶ Do the exercises

Exercise 1 — Data Within Data

Imagine representing a full trainer profile: the trainer is an object with a `team` array, where each Codémon is an object with its own `moves` array! Deeply nested data looks complex at first glance, but reading it follows one simple rule:

const trainer = { name: "Sam", team: [ { name: "Aquachu", moves: ["Splash", "Wave"] }, { name: "Zap", moves: ["Spark"] } ] }; console.log(trainer.team[0].moves[1]);

Always read the chain from left to right, one step at a time: 1. `trainer` → the trainer object 2. `.team` → its array of Codémon 3. `[0]` → the first Codémon object (`Aquachu`) 4. `.moves` → Aquachu's array of moves 5. `[1]` → the second move (`"Wave"`)

Exercise 2 — Breaking Long Chains Into Steps

Long chains like `trainer.team[0].moves[0]` can get hard to read and debug. You never have to write everything in a single line — store intermediate steps in descriptive variables instead:

const trainer = { name: "Sam", team: [ { name: "Aquachu", moves: ["Splash", "Wave"] } ] }; const starter = trainer.team[0]; const firstMove = starter.moves[0]; console.log(starter.name + " uses " + firstMove);

The output is identical, your code is much easier to read, and if a bug happens you can `console.log()` each intermediate variable to spot the problem immediately.

Exercise 3 — Quiz: Follow the Path

What does this code print?

const gym = { leader: { name: "Brock", team: ["Rockachu", "Metachu"] } }; console.log(gym.leader.team[1]);
Options
• Metachu
• Rockachu
• "Brock"
• undefined

Hint : Take one step at a time, from left to right.

Exercise 4 — Reach the Move

Print the first move of the first team member.

Hint : Positions use one kind of bracket, properties use their name after a dot.

Exercise 5 — Looping Through Two Levels

To visit everything, put one loop inside another. The outer loop walks the team, the inner one walks each member's moves:

const team = [ { name: "Aquachu", moves: ["Splash", "Wave"] }, { name: "Zap", moves: ["Spark"] } ]; team.forEach((cdm) => { console.log(cdm.name + ":"); cdm.moves.forEach((move) => { console.log(" - " + move); }); });

Each turn of the outer loop runs the whole inner loop. Here that's two moves for Aquachu, then one for Zap.

Exercise 6 — Quiz: How Many Lines?

How many lines does the inner loop print in total?

const team = [ { moves: ["a", "b"] }, { moves: ["c"] }, { moves: ["d", "e"] } ]; team.forEach((cdm) => { cdm.moves.forEach((m) => console.log(m)); });
Options
• 5
• 3
• 2
• 6

Hint : Add up the sizes of the inner arrays.

Exercise 7 — Where Chains Break

A missing link doesn't give `undefined`, it gives an error. Asking `undefined` for a property crashes the program:

const trainer = { name: "Sam" }; console.log(trainer.team); // console.log(trainer.team[0]); // Uncomment this line to see the error!

The first line is fine: `team` is simply missing. The second would try to read index 0 of nothing. Checking before going deeper avoids it:

const trainer = { name: "Sam" }; if (trainer.team) { console.log(trainer.team[0]); } else { console.log(trainer.name + " has no team yet"); }

Exercise 8 — Check Before Diving

Print the first item only if the bag exists.

Hint : Test the very link you are about to follow.

Exercise 9 — Reading a Path

Match each step of `a.list[0].name` with what it does.

Items to match
• a
• .list
• [0]
• .name
Definitions
• The starting object
• A property holding an array
• The first element of that array
• A property of that element

Exercise 10 — Move Inventory

Produce a full move inventory for a trainer, walking both levels of the data.

Step 1: print `"Trainer: name"` from the trainer object. Step 2: for each team member, print `"name knows N move(s)"`, then each of its moves on its own line with `" - "` in front. Step 3: while looping, count every move of the whole team, and print `"Total moves: N"` at the end.

Expected output:

Trainer: Sam Aquachu knows 2 move(s) - Splash - Wave Zap knows 1 move(s) - Spark Total moves: 3
Starter code
const trainer = { name: "Sam", team: [ { name: "Aquachu", moves: ["Splash", "Wave"] }, { name: "Zap", moves: ["Spark"] } ] }; // Write your code here
Back to the course