Map and Set in JavaScript
Keyed collections with any key type, and unique sets.
const cache = new Map();
cache.set("ada", 95);
cache.set("grace", 88);
const score = cache.get("ada");
const known = cache.has("kay");
cache.delete("grace");
const size = cache.size;
const tags = new Set(["a", "b", "a", "c"]);
tags.add("d");
const unique = [...tags];
const hasB = tags.has("b");
How it works
Maphasset,get,has,delete, andsize.Setstores unique values; duplicates collapse.- Spreading a Set yields a deduplicated array.
Keywords and builtins used here
MapSetconst
The run, in numbers
- Lines
- 13
- Characters to type
- 297
- Tokens
- 102
- Three-star pace
- 95 tpm
At the three-star pace of 95 tokens a minute, this run takes about 64 seconds.
Step 3 of 8 in Objects & collections, step 29 of 43 in Language basics.