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

Read the Shape of Nested Data

In this lesson, the year-end report moved from Maya alone to the whole Riverside Book Club. You drew the club’s log one level at a time, traced a path down the drawing to a single rating, read the error a broken path gives, and saw why each level is an array or an object.

Key Points

  • When you print nested data with console.log(), Node only goes so deep. It shows each member’s name and books list, but every book shows up as [Object], so draw the shape yourself to see every level, and read the bottom level, the book records, from the file.
  • The log has four levels: log is an array of members, each member is a plain object with a name and books, books is an array, and each book is a plain object with a title, an author, a genre, a rating and sometimes a review. Luis joined in December, so Luis’s books array is empty.
  • A path takes one step per level: an index in brackets for an array, a property name after a dot for an object. log[1].books[0].rating goes to Jamal’s record, then his books, then his first book, The Thursday Murder Club, then its rating, 5. log[2].books[1].title is the second book in Rosa’s list, "Little Fires Everywhere".
  • When a step in the middle of a path doesn’t match the shape, it gives undefined, and the step after it throws. log[1].book[0].rating stops with TypeError: Cannot read properties of undefined (reading '0'). The message names the step JavaScript was trying to take, 0, so the step right before it, .book, is the broken one. A member has books, with an s.
  • Watch out for a path that skips a level. log is an array, so log.books is undefined, and log.books[0] throws. Start with an index, such as log[1].
  • The members are an array because they’re many things of the same kind, and the report goes through all of them in order. To look members up by name, a lookup table would fit instead, like the genre labels. Each member is an object because its parts are different things, reached by name. member.name says what it holds, and member[0] doesn’t. The same holds one level down for books and each book.

Final Code

The script report.js holds the club’s full log and reads two values from it. Copy it to follow along with the video:

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 So far, the report has worked on Maya alone. Now it needs the whole club: six members, each with a list of books. The club keeps them in one array, called log, in a new script, which is below this video.

00:14 Before you write a loop over it, you need its shape, so print it. Node shows the members, one per line, each with a name and a books list. But every book shows up as the word Object in square brackets.

00:27 Node only prints nested data so deep, so the book records stay hidden.

00:33 To see every level, draw the shape yourself, one level at a time.

00:39 Start at the top. The printout opens with a square bracket, so log is an array. It holds six items, one per member.

00:49 Go one level down. Each item starts with a curly brace, so it’s an object: a member record, with a name and books. One more level. books is an array again, a list of that member’s books.

01:03 Luis joined in December, so Luis’s list is empty.

01:07 The printout stops there, so read the file instead. Each book is an object, with a title, an author, a genre and a rating. Some also have a review. That’s four levels: an array, an object, an array and an object.

01:26 You’ve written paths like this for Maya’s books, one level down per step. Now suppose the report needs the rating of Jamal’s first book. Write the path one step per level, reading it off the drawing.

01:39 The log is an array, and Jamal is the second member, so the first step is index one. That lands on Jamal’s record, an object, so the next step is the name books, after a dot.

01:52 That’s an array, so the next step is index zero: The Thursday Murder Club. And that’s an object, so the last step is its rating. Run it, and it logs 5.

02:05 Here’s a second path. Your turn: pause the video and predict what it logs.

02:15 It logs Little Fires Everywhere. Index two is Rosa, and index one is the second book in her list.

02:25 A path breaks when one step doesn’t match the shape. Suppose you type book, without the s. Run it, and Node stops with a TypeError: it can’t read properties of undefined, reading ‘0’.

02:39 Read that from the end. Zero is the step JavaScript was trying to take. Undefined is what it tried to take that step on. So the step right before zero gave undefined.

02:51 On the drawing, a member record has books, with an s, and no book property at all. Fix that step, and the path works again. Watch out: the log is an array, so going straight to books without an index gives undefined, and the next step throws.

03:11 Why is the log shaped like this? The members are an array because they’re many things of the same kind. The report goes through all of them in order, so a position is all it needs.

03:21 To look members up by name, a lookup table would fit, like the genre labels. Each member is an object because its parts are different things: a name and a list of books.

03:32 You could store a member as an array, with the name at position zero. That works, but zero doesn’t say what’s there. A name does. The same goes one level down: books is a list, and each book has named parts.

03:49 Here’s the rule: draw the shape before you write the path, and name the kind of value at each level. The path then takes one step per level: an index for an array, a property name for an object.

04:03 Six members is a toy log, but bigger data nests the same way. Next, in Loop Through Records With Lists, you’ll visit every level with loops, instead of one path at a time.

Become a Member to join the conversation.