Chapter 3 · Data

Maps and sets

You are reading the whole lesson on one page. Back to one step at a time

Step 1 of 21 · Concept

A Map looks values up by key

By the end of this lesson you look values up by key, count things, and keep each value once.

const ages = new Map();
ages.set("Ada", 36);
ages.set("Alan", 41);

A Map holds keys, each with its value.

writedoes
ages.set(k, v)puts v under k; again, it replaces the value
ages.get(k)the value, or undefined when k is missing
ages.has(k)true when k is there
ages.delete(k)removes k and its value
ages.sizehow many keys
const ages = new Map();
ages.set("Ada", 36);
ages.set("Alan", 41);
ages.set("Ada", 37);
console.log(ages.size, ages.get("Grace"));
const ages = new Map();
ages.set("Ada", 36);
ages.set("Alan", 41);
ages.set("Grace", 85);
ages.set("Ada", 37);
console.log(ages);
for (const [k, v] of ages) {
  console.log(`${k} is ${v}`);
}
const stock = new Map();
stock.set("tea", 3);
stock.set("cake", 1);
const sales = ["tea", "cake", "cake", "juice"];
// 1. each sale: none left? log No and the item
// 2. else take 1 from its count
// 3. log each item and its count
const line = "the cat the hat the";
const counts = new Map();
for (const w of line.split(" ")) {
  counts.set(w, (counts.get(w) ?? 0) + 1);
}
const top = [...counts].sort((a, b) => b[1] - a[1]);
for (const [w, n] of top) {
  console.log(w, n);
}
import { readFileSync } from "node:fs";

const text = readFileSync(0, "utf8");
// 1. cut the text into words
// 2. count each word with a Map
// 3. log each word and its count, most first
const tags = ["js", "web", "js", "css", "web"];
const unique = new Set(tags);
unique.add("js");
console.log(unique.size);
console.log([...unique].join(" "));
const chess = ["Ada", "Bob", "Cy"];
const music = ["Cy", "Di", "Ada"];
// 1. a Set of the music club's names
// 2. the chess players in that Set too
// 3. how many people in at least one club
const names = ["Ada", "Alan", "Bo", "Bea", "Cy"];
const groups = new Map();
// add each name to its first letter's array
for (const [letter, list] of groups) {
  console.log(`${letter}: ${list.join(", ")}`);
}
Terminal
Step 1 of 8Size and a missing key

Keyboard shortcuts

The step

ContinueEnter
Previous / next stepAlt+[ · Alt+]
Move along the steps bar← → Home End, then Enter
From a quiz's title to its answers↓
Skip to the stepTab, then Enter (first on the page)
Course outlineAlt+O
These shortcuts?

The workspace

Run the stepCtrl+Enter (⌘↵ on a Mac)
Lesson → editor → consoleF6 (Shift+F6 back)
Output ↔ TerminalCtrl+`
Focus the editorCtrl+. (⌘. on a Mac)
Hide or show the lessonAlt+L
Restore, close a messageEsc (outside the editor and the terminal)

The editor

Leave the editorF6 (Shift+F6 back); or Ctrl+M (⌃⇧M on a Mac), then Tab moves the focus (not in the vim mode)
Open a file by nameCtrl+P (⌘P on a Mac)
All commandsCtrl+Shift+P (⌘⇧P on a Mac) or F1
Go to a lineCtrl+G (⌃G on a Mac)
Show or hide the filesCtrl+B (⌘B on a Mac)
Previous / next file tabCtrl+Shift+[ · Ctrl+Shift+] (⌘⇧[ · ⌘⇧] on a Mac)
The file tabs← → Home End on a tab; Delete closes it
Save the files nowCtrl+S (⌘S on a Mac)
Go to a symbol in the fileCtrl+Shift+O (⌘⇧O on a Mac)
Go to the definitionF12
Format the fileCtrl+Shift+F (⌘⇧F on a Mac), or Shift+Alt+F
Go back / forward after a jumpAlt+← · Alt+→ (⌃- · ⌃⇧- on a Mac)