JavaScript AcademyProfessional Engineering

Learning object 32 of 100

Map collection visualizer

Step through building Maps for counting, grouping and lookups, and watch entries being added and updated.

Loading…

What it shows

A Map stores key-value pairs with any type of key, remembers insertion order and knows its size. The counting pattern is map.set(key, (map.get(key) ?? 0) + 1). Use get and set: bracket assignment creates ordinary properties that the Map does not see.

Guided experiment

  1. In Word count, step until "the" reaches 2.
  2. In Group by, add an item with a new category.
  3. In Brackets trap, compare cart.size with the property that was created.

Predict

After const m = new Map(); m.set("a", 1); m.set("a", 2); what is m.size?

Related

Lessons: Lesson 45: Sets, Maps and modern collection patterns

Projects: Project 9: Shopping Cart Price Calculator

Source: web/assets/js/objects/object-032.js in the companion repository.