Learn / Lesson 5 of 10
Objects and Maps
Look things up by name instead of by position.
Words on this page, in plain English (14)Open if any word is new to you
- Variable
- A labelled box that holds a value. You give it a name, put something in it, and use the name later to get it back.
- String
- A piece of text, written between quotes. Each letter in it is a character.
- Boolean (true or false)
- A value that can only be
trueorfalse. Like a light switch: on or off. - Array (list)
- A list of values kept in order, inside square brackets. Like a row of numbered lockers.
- Index (position)
- Where an item sits in a list. Counting starts at 0, not 1, so the first item is at position 0.
- If (a decision)
- Runs some code only when something is true. Like: if it's raining, take an umbrella.
- Loop
- Code that repeats. A
forloop repeats once per item or a set number of times; awhileloop repeats as long as something stays true. - console.log
- Prints something so you can see it. The easiest way to find out what your code is really doing.
- Object
- A bundle of named values, in curly braces. Like a form with labelled boxes.
- Map
- A lookup table: it stores pairs of a key and a value, and finds any key instantly. Like a coat check: hand over a ticket, get your coat straight back.
- Set
- A collection that only remembers which things it has seen, with no repeats. Like a guest list: a name is either on it or not.
- Key
- The label you look something up by in a Map or object, like a word in a dictionary.
- Hash, hashing
- A quick calculation that turns a key into a shelf number, so a Map can jump straight to it instead of searching.
- Pattern
- A trick that solves a whole family of problems. Learn the trick once, and you can solve problems you've never seen.
Looking things up by name
Arrays find values by position. But often you want to find a value by name: a person's phone number by their name, a word's count by the word. For that, use a key and a value.
Objects
An object groups named values, called properties, in curly braces:
Try it: an object
const book = {
title: "Dune",
pages: 412,
finished: false,
};
console.log(book.title); // read a property with a dot
console.log(book["pages"]); // or with brackets and a string
book.finished = true; // change one
book.rating = 5; // add a new one
console.log(book);Press Run to see the output. You can change the code first.
Objects are perfect for describing one thing with a fixed set of properties, like a book, a user, or a point { x: 3, y: 4 }.
Maps
When the keys aren't known in advance, or there are lots of them (every word in a book, every price seen so far), use a Map. It's built for adding, finding and removing keys:
Try it: a Map
const phone = new Map();
phone.set("Ana", "555-0101");
phone.set("Ben", "555-0199");
console.log(phone.get("Ana")); // look up by key
console.log(phone.has("Cleo")); // is this key there?
console.log(phone.size); // how many keys
phone.delete("Ben");
console.log(phone);Press Run to see the output. You can change the code first.
Unlike object properties, Map keys can be any value: numbers, strings, even objects. And get on a missing key gives undefined.
Counting with a Map
This is one of the most useful patterns in all of programming. To count how many times each thing appears:
Try it: counting words
const words = ["cat", "dog", "cat", "bird", "cat", "dog"];
const counts = new Map();
for (const word of words) {
const soFar = counts.get(word) ?? 0; // 0 if we haven't seen it
counts.set(word, soFar + 1);
}
console.log(counts);Press Run to see the output. You can change the code first.
?? means "if the left side is undefined (or null), use the right side instead". So counts.get(word) ?? 0 is "the count so far, or 0 for a new word".
Sets
A Set is like a Map with only keys: it remembers which values it has seen, and each value at most once.
Try it: a Set
const seen = new Set();
seen.add("red");
seen.add("blue");
seen.add("red"); // already there: no change
console.log(seen.size);
console.log(seen.has("blue"));
// A quick way to remove repeats from an array:
console.log([...new Set([3, 1, 3, 2, 1])]);Press Run to see the output. You can change the code first.
Which one?
| Use | When |
|---|---|
| Array | an ordered list, found by position |
| Object | one thing with a few named properties |
| Map | many keys you look up, add or remove as you go |
| Set | "have I seen this?" with no extra information |
The big win of Maps and Sets: get, has, set and add are instant, however many keys there are. Searching an array is not. The Hashing pattern is built on exactly this.
Your turn
Count how many times each letter appears in "mississippi", using a Map. Then show which letter is most common.
Your turn
const word = "mississippi";Press Run to see the output. You can change the code first.
What you learned
- Objects hold named properties:
book.title. - Maps hold any number of key → value pairs:
set,get,has,delete,size. - Sets hold unique values:
add,has,size. - Counting with a Map:
counts.set(x, (counts.get(x) ?? 0) + 1).