Build Counts With .reduce()

In the last lesson, you added up Rosa’s ratings with .reduce(), starting from 0, and watched each call’s return value become the next call’s accumulator. A number is only one kind of result, though. The year-end report also needs to know how many books the club read in each genre and the club’s highest rating.

In this lesson, you’ll build both with .reduce() over allBooks, the list of every member’s books you collected with a nested loop. For the genre count, you’ll start from an empty object instead of 0. You’ll compare .reduce() with loops and shorter alternatives, so you can pick the clearest one for a job. Start with the genre count, the tally you already know how to write with a loop.

Count Genres Into an Object

The report’s genre count covers the whole club, so it works on allBooks, the flat array of every member’s book records. Here’s its shape, showing only the properties this lesson reads, along with the shape of the finished genre count:

allBooks: array of 15 book objects
  [0]  { title: "Kindred",     genre: "sci-fi",  rating: 5, ... }
  [1]  { title: "The Martian", genre: "sci-fi",  rating: 4, ... }
  [2]  { title: "Circe",       genre: "fantasy", rating: 3, ... }
  ...
  [14] { title: "Kindred",     genre: "sci-fi",  rating: 4, ... }

genre counts: object, one key per genre
  { "sci-fi": number, fantasy: number, ... }

The list goes in, and one object comes out. That’s the change from the rating total. The accumulator is now that object instead of one number, and it gains a key or a bigger count with each book. As a loop, the tally from Count and Look Up With an Object looks like this:

Language: JavaScript
const counts = {};
for (const book of allBooks) {
  counts[book.genre] = (counts[book.genre] ?? 0) + 1;
}

As with the rating total, the loop starts with a value before the first book, updates it for each book, and ends with one result. That’s the job .reduce() does, starting from {} instead of 0:

Language: JavaScript
const genreCounts = allBooks.reduce((counts, book) => {
  counts[book.genre] = (counts[book.genre] ?? 0) + 1;
  return counts;
}, {});
console.log(genreCounts);

The accumulator, counts, starts as the {} after the callback. The tally line updates it, and return counts; hands the same object on to the next call. After the callback processes the last book, .reduce() gives back that object, and genreCounts names it.

This callback has a body in braces because it needs two statements. It returns only what a return statement gives it, the same rule as for any function you write.

Watch out: without return counts;, the first call returns undefined. The second call then gets undefined as counts, and the script stops with TypeError: Cannot read properties of undefined (reading 'sci-fi'). Write the return line first, and then fill in the update above it.

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

Become a Member to join the conversation.