Keep Distinct Values in a Set

In the last lesson, you counted the club’s books by author in a Map, and its .size told you the club read seven different authors. The report’s next lines need no counts, though. One could list those authors, each once, and another could name the authors two members share.

In this lesson, you’ll collect values into a container that holds each value once, check whether a value is in it, and compare two such containers. You’ll also be able to say when it fits better than an array or a Map. Start with the report’s list of authors and the shape of the data it comes from.

Collect Each Author Once

The author list comes from allBooks, the flat array of every member’s book records, where an author appears once per book. Here’s its shape, showing each book’s title and author, and the list it should become:

allBooks: array of 15 book objects
  [0]  { title: "Kindred",     author: "Octavia E. Butler", ... }
  [1]  { title: "The Martian", author: "Andy Weir", ... }
  ...
  [14] { title: "Kindred",     author: "Octavia E. Butler", ... }

authors: 7 strings, each author once, no positions needed
  "Octavia E. Butler", "Andy Weir", "Madeline Miller", ...

Octavia E. Butler turns up three times in allBooks, but the list should name her once. Why not take the keys of the last lesson’s Map? You could, but a Map pairs every key with a value, and this list has no values to pair.

A Set is a built-in object that holds values, each one at most once. You create an empty one with new Set(), as you did with new Map(), and .add(value) puts a value in. Adding a value the Set already holds changes nothing, so you never need to check first. Like a Map, a Set keeps its .size up to date and remembers the order values first arrived in.

Open report.js. Keep the log and the loop that builds allBooks, and delete the Map work from the last lesson. Then collect the authors with the loop from your Map tally, minus the counting:

Language: JavaScript
const authors = new Set();
for (const book of allBooks) {
  authors.add(book.author);
}
console.log(authors.size);
console.log(authors);
console.log(authors.has("Celeste Ng"));
console.log(authors.has("Jane Austen"));

Like a Map’s .has(), the .has(value) method returns true or false. Here, it tells you whether the club read an author at all.

A Set has no positions, though. Watch out for reading it like an array, because authors[0] and authors.length are both undefined. When the report needs positions, turn the Set into an array with spread syntax. Spread lays out a Set’s values in order, and a for...of loop visits them in that same order.

Spread and a Set together remove repeats from any array in one line, [...new Set(items)], because new Set() also accepts an array and adds each item in turn. Add these lines to the end of the file. The second pair builds the club’s list of genres:

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.