JavaScript, inside out · Chapter 33 of 51

Sets and maps

By the end of this chapter you can keep a group of values with no repeats, compare two groups, and look values up by any kind of key, even an object. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Modern JavaScript
  • 5 tasks
  • JavaScript
  • In the all labs plan

The first 2 chapters of JavaScript, inside out are free. This one comes with the all labs plan.

Why you need this

Meera wants numbers: how many different things were sold today, who came on both Monday and Tuesday, and how many of each item went out. Ravi keeps a running tab for the regulars, and last week two customers called Ravi ended up on one tab. Anjali wants the loyalty cup count kept where nobody can edit it.

With what you know so far, a group is an array, and checking whether a name is already in it means includes, which reads every element, every time. A lookup is a plain object, and an object turns every key into a string. Two customer objects both become the text '[object Object]', so they share one tab. And any key you put on an object can be read and changed by anyone holding the object.

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1 · interview question

    Interview: uniqueItems, remove duplicates in one line

    Write uniqueItems(items). Return a new array (an array, not a Set) with every duplicate removed, keeping each item where it first appears: ['masala chai', 'bun maska', 'masala chai', 'Parle-G'] gives ['masala chai', 'bun maska', 'Parle-G']. The array you are given must not change.

    An empty array gives [].

  2. Task 2

    union, intersection, difference: two days of customers

    Write union(a, b), intersection(a, b) and difference(a, b). Each takes two Sets and returns a new Set, never one of the two you are given, and neither input may change.

    union holds everyone in a or b: a's members first, in a's order, then b's members that a does not have, in b's order. intersection holds everyone in both, in a's order. difference holds everyone in a who is not in b, in a's order.

    For a = Ravi, Anjali, Kabir, Meera and b = Anjali, Dev, Meera: union gives Ravi, Anjali, Kabir, Meera, Dev; intersection gives Anjali, Meera; difference gives Ravi, Kabir, and difference(b, a) gives Dev.

  3. Task 3

    countByItem: how many of each item sold, in a Map

    Write countByItem(sold). sold is an array of item names, one per item sold. Return a Map from each item to how many times it was sold, with the items in the order they were first sold: ['bun maska', 'masala chai', 'bun maska'] gives a Map of 'bun maska' to 2 and 'masala chai' to 1.

    An empty list gives an empty Map (size 0).

  4. Task 4

    makeTabs: a running tab per customer, even with the same name

    Fix makeTabs(). It returns an object with four methods, keyed by the customer object itself, not by name: add(customer, amount) adds to that customer's tab and returns the new total; owed(customer) gives that customer's total, or 0 when they have no tab; settle(customer) clears their tab and returns what they owed; count() gives how many customers have a tab now.

    It is broken now: a plain object turns every customer object into the same text key, so two customers share one tab. Two different objects with the same name, such as two customers called Ravi, must keep separate tabs, and settling one leaves the other alone.

  5. Task 5

    LoyaltyCard: cups hidden in a WeakMap

    Change LoyaltyCard so that its only own property is name: keep the cup count in a WeakMap made outside the class, keyed by the card. Reflect.ownKeys of a card gives ['name'], and JSON.stringify of Anjali's card gives '{"name":"Anjali"}'.

    A new card has 0 cups. addCup() adds one cup and returns the new count, cups() gives the current count, and freeChaiDue() is true once the card has 10 or more cups. Each card keeps its own count.

What you will learn

  • Set: a group that holds each value once, in the order added.
  • Set operations: union, intersection and difference of two sets.
  • Map: a lookup whose keys can be anything, objects included.
  • WeakMap: private data attached to an object from outside it.

More in Modern JavaScript

Build "Sets and maps" yourself

Tapri, a chai brand going online, needs its menu, orders and kitchen in code. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of JavaScript, inside out are free. This one comes with the all labs plan.