Summarize Each Group

Your genre groups from the last lesson hold the books themselves, six under "sci-fi", two under "fantasy", and so on. A genre page in the year-end report doesn’t print individual book records, though. Instead, it prints a summary: how many books the club read, their average rating, and their favorite book.

In this lesson, you’ll turn each group into a summary with tools you already have: .length, .reduce(), and a function you write once and call per genre. You’ll also make the report list every genre the club follows, including one that nobody read this year. Start by printing the shape you have and sketching the shape you want.

Count and Average Each Group

Each group is an array, but the report needs a few numbers per genre instead. Open report.js and keep the log, the loop that builds allBooks, and the Object.groupBy() call that builds byGenre. Delete the rest, including the byVerdict grouping. Then add one line to look at what goes in:

Language: JavaScript
console.log(Object.keys(byGenre));

When you run the script, it prints [ 'sci-fi', 'fantasy', 'memoir', 'mystery', 'fiction' ], one key per genre that occurred. Here’s that shape before and after summarizing:

byGenre: object, one key per genre
  "sci-fi": array of 6 book objects
  fantasy:  array of 2 book objects
  ...

summaries: object, one key per genre
  "sci-fi": { count: 6, average: 4.5 }
  fantasy:  { count: 2, average: ... }
  ...

The keys stay the same, but each array of books becomes one small record of figures, a plain object like each book. Summarizing a group means reducing its records to values like a count, a total, an average, or a top item. It’s the same work you did on the whole of allBooks, done once per group.

The count is the group’s .length, and the average is its rating total divided by that count. Replace the Object.keys() line with a loop over the entries, which hands you each genre and its books together:

Language: JavaScript
const summaries = {};
for (const [genre, books] of Object.entries(byGenre)) {
  const total = books.reduce(
    (sum, book) => sum + book.rating,
    0,
  );
  summaries[genre] = {
    count: books.length,
    average: total / books.length,
  };
}
console.log(summaries);

Inside the loop, books is one genre’s array. The .reduce() call adds up its ratings from a starting value of 0, as it did for Rosa’s books. The last statement stores a record of the two figures under the genre’s key, so summaries ends up as an object of objects.

Your file should now hold the log, allBooks, byGenre, and the summary loop:

Language: JavaScript Filename: report.js
const log = [
  {
    name: "Maya",
    books: [
      {
        title: "Kindred",
        author: "Octavia E. Butler",
        genre: "sci-fi",
        rating: 5,
        review: "Couldn't put it down.",
      },
      { title: "The Martian", author: "Andy Weir", genre: "sci-fi", rating: 4 },
      {
        title: "Circe",
        author: "Madeline Miller",
        genre: "fantasy",
        rating: 3,
        review: "Slow start, lovely ending.",
      },
      { title: "Educated", author: "Tara Westover", genre: "memoir", rating: 4 },
    ],
  },
  {
    name: "Jamal",
    books: [
      {
        title: "The Thursday Murder Club",
        author: "Richard Osman",
        genre: "mystery",
        rating: 5,
        review: "Funny and clever.",
      },
      { title: "Born a Crime", author: "Trevor Noah", genre: "memoir", rating: 3 },
      {
        title: "The Man Who Died Twice",
        author: "Richard Osman",
        genre: "mystery",
        rating: 4,
      },
    ],
  },
  {
    name: "Rosa",
    books: [
      {
        title: "Parable of the Sower",
        author: "Octavia E. Butler",
        genre: "sci-fi",
        rating: 5,
        review: "Hard to read, harder to forget.",
      },
      {
        title: "Little Fires Everywhere",
        author: "Celeste Ng",
        genre: "fiction",
        rating: 4,
      },
      {
        title: "Project Hail Mary",
        author: "Andy Weir",
        genre: "sci-fi",
        rating: 5,
      },
    ],
  },
  {
    name: "Dev",
    books: [
      {
        title: "Project Hail Mary",
        author: "Andy Weir",
        genre: "sci-fi",
        rating: 4,
      },
      {
        title: "Circe",
        author: "Madeline Miller",
        genre: "fantasy",
        rating: 0,
        review: "Gave up halfway.",
      },
    ],
  },
  {
    name: "Hannah",
    books: [
      { title: "Educated", author: "Tara Westover", genre: "memoir", rating: 5 },
      { title: "Born a Crime", author: "Trevor Noah", genre: "memoir", rating: 4 },
      { title: "Kindred", author: "Octavia E. Butler", genre: "sci-fi", rating: 4 },
    ],
  },
  { name: "Luis", books: [] },
];

const allBooks = [];
for (const member of log) {
  for (const book of member.books) {
    allBooks.push(book);
  }
}

const byGenre = Object.groupBy(
  allBooks,
  (book) => book.genre,
);

const summaries = {};
for (const [genre, books] of Object.entries(byGenre)) {
  const total = books.reduce(
    (sum, book) => sum + book.rating,
    0,
  );
  summaries[genre] = {
    count: books.length,
    average: total / books.length,
  };
}
console.log(summaries);

Run the script, and Node prints one summary record per genre:

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.