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

Loop Through Records With Lists

In this lesson, the Riverside Book Club’s report went through every member and every book. You used one loop for the members and one inside it for each member’s books, totaled each member’s ratings, and collected the whole club’s books into one list, allBooks, which later parts of the report reuse.

Key Points

  • Read the shape before you write the loops. log is an array of members, so the outer loop goes through the members. Each member’s books is an array too, so the inner loop goes through member.books.
  • A member’s book count is already there, as member.books.length. Together with a rating total, it gives an average later in the report. The rating total needs the inner loop, which adds each book’s rating to a running total.
  • A figure for each member starts at the top of the outer body, so each member starts fresh, and the outer body logs it after the inner loop finishes. Maya gets 4 books and 16 points, Jamal 12 points, and Dev, who gave up on Circe, 4.
  • Luis has no books, so his inner loop runs zero times, and his total stays at 0.
  • Watch out: if the total starts above the outer loop, it never starts over, and each member’s figure includes everyone before them. Jamal then shows 28 points: Maya’s 16 plus his 12.
  • A figure for the whole club starts above both loops. allBooks is created once, and the inner body pushes each book onto it, so it holds all 15 books: four for Maya, then three, three, two and three, and none for Luis.
  • allBooks holds the same book objects, not copies: allBooks[0] === log[0].books[0] is true, so a change to that book shows up through either one.
  • A club-wide job can loop once over allBooks or use the two loops over log. Either way, its result starts above every loop it runs through. One loop over allBooks, with a check on the genre around the push, collects the six sci-fi books, and .map() turns them into their titles so the output stays short. Kindred and Project Hail Mary appear twice, because two members each have their own record of each book.
  • The highest-rated book is found across both levels, with the two loops over log. The best so far starts above both loops as null, meaning no book yet. In the inner body, a book replaces it if best is still null or if its rating is higher, so the first book found becomes the best even if the first member read nothing.
  • The answer is Kindred, rated 5. Four other books are rated 5 too, but none is higher, so Kindred stays. The club has books, so best isn’t null by the end.

Final Code

The script report.js holds the club’s full log from the previous lesson. Below it, in place of the two path lines, are the four jobs from the video. The video showed each job on its own, but here they sit one after another in one file, so one run prints every result. The sci-fi job needs allBooks, so it comes after the loop that builds it, and the highest-rated job goes back to the two loops over log. Copy it to follow along:

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 You’ve drawn the club’s log, one level at a time. Now the report needs two figures for each member: how many books they read, and their ratings added up. Those are the two numbers an average needs.

00:13 Read the shape first. The log is an array of members, so one loop goes through the members. Each member’s books list is an array too, so a second loop, inside the first, goes through that member’s books.

00:28 The outer loop takes one member per pass. You already have the book count. It’s the length of that member’s books. The rating total needs the inner loop, which adds each book’s rating to a running total.

00:41 That total starts at zero at the top of the outer body, so each member starts fresh. After the inner loop finishes, the outer body logs the member’s line, with the book count in it.

00:54 Run it, and Maya gets four books and sixteen points. Jamal gets twelve, and Dev, who gave up on Circe, gets four. Luis has no books, so his inner loop runs zero times, and his total stays at zero.

01:10 Watch out: if the total starts above the outer loop, it never starts over. Each member’s figure then includes everyone before them, so Jamal shows twenty-eight: Maya’s sixteen plus his twelve.

01:25 Later parts of the report need every book in the club in one list. That list belongs to the whole club, not to one member. So you create it once, above both loops.

01:37 The loops stay the same. In the inner body, each book gets pushed onto the list. After the loops, the script logs how many books the list holds. Your turn: pause the video and predict the number it logs.

01:56 It logs fifteen. The inner body runs once per book: four for Maya, then three, three, two and three, and zero for Luis.

02:08 And the list holds the same book objects, not copies. Compare its first item with Maya’s first book using triple equals, and you get true. Both reach one object, so a change to that book shows up through either one.

02:24 A club-wide job can loop once over allBooks, or use the two loops over the log. Either way, its result starts above every loop it runs through. To collect the sci-fi books, loop over allBooks, and put the push inside a check on the genre.

02:40 The log line maps them to their titles, to keep the output short. That gives six books, and Kindred and Project Hail Mary appear twice, because two members each have their own record of each one.

02:53 To find the highest-rated book, go back to the two loops over the log. Above both, the best so far starts as null, meaning no book yet. In the inner body, a book replaces it if best is still null, or if its rating is higher.

03:09 So the first book found becomes the best, even if the first member read nothing. The club has books, so best isn’t null by the end. The answer is Kindred, rated five.

03:20 Four other books are rated five too, but none is higher, so nothing replaces Kindred.

03:28 Here’s the rule: write one loop for each level that’s a list, and start each value at the level it belongs to. A figure for each member starts in the outer body.

03:38 A figure for the whole club starts above the loops. Six members is a toy log, but the same loops work for a club of hundreds. You’ll reuse allBooks in later parts of the report.

03:51 Next, in Work With Grids of Rows, you’ll loop through data stored as rows and columns.

Become a Member to join the conversation.