Skip to main content

Map

Definition

A Map is a collection of key-value pairs where keys can be any type (unlike objects where keys must be strings or symbols). Maps maintain insertion order and provide efficient lookups.

> const map = new Map()
undefined

> map instanceof Map
true

Using maps

// Empty map
const map = new Map()

// From array of [key, value] pairs
const map2 = new Map([
['name', 'Alice'],
['age', 30]
])

// From another map
const map3 = new Map(map2)

Map methods

Setting and Getting

const map = new Map()

// set() - adds or updates a key-value pair
map.set('name', 'Alice')
map.set('age', 30)

// get() - retrieves value by key
map.get('name') // 'Alice'
map.get('age') // 30
map.get('city') // undefined (key doesn't exist)

// has() - checks if key exists
map.has('name') // true
map.has('city') // false

Size

const map = new Map([['a', 1], ['b', 2]])
map.size // 2

Deleting

const map = new Map([['a', 1], ['b', 2]])

// delete() - removes a key-value pair
map.delete('a') // true (key existed)
map.delete('c') // false (key didn't exist)

// clear() - removes all entries
map.clear()
map.size // 0

Operations on maps

Iteration

for...of Loop

const map = new Map([['name', 'Alice'], ['age', 30]])

// Iterate over entries (default)
for (const [key, value] of map) {
console.log(key, value)
}

// Iterate over keys
for (const key of map.keys()) {
console.log(key)
}

// Iterate over values
for (const value of map.values()) {
console.log(value)
}

forEach()

const map = new Map([['name', 'Alice'], ['age', 30]])

map.forEach((value, key) => {
console.log(key, value)
})

entries(), keys(), values()

const map = new Map([['name', 'Alice'], ['age', 30]])

Array.from(map.entries()) // [['name', 'Alice'], ['age', 30]]
Array.from(map.keys()) // ['name', 'age']
Array.from(map.values()) // ['Alice', 30]

Map vs object

FeatureMapObject
Key typesAny typeString or Symbol
Sizemap.sizeManual calculation
IterationDirectly iterableRequires Object.keys()
PrototypeNo default keysHas default keys
PerformanceBetter for frequent additions/deletionsBetter for small, static data

Use cases

Using Non-String Keys

const map = new Map()

const objKey = { id: 1 }
const funcKey = () => {}

map.set(objKey, 'value1')
map.set(funcKey, 'value2')

map.get(objKey) // 'value1'

Counting Occurrences

const words = ['hello', 'world', 'hello']
const count = new Map()

words.forEach(word => {
count.set(word, (count.get(word) || 0) + 1)
})
// Map { 'hello' => 2, 'world' => 1 }

Caching

const cache = new Map()

function expensiveOperation(input) {
if (cache.has(input)) {
return cache.get(input)
}

const result = /* expensive computation */
cache.set(input, result)
return result
}

Best practices

  1. Use Map for dynamic key-value pairs: When you frequently add/remove entries.

  2. Use Object for static data: When you have a fixed set of known properties.

  3. Use Map for non-string keys: When you need objects or functions as keys.

  4. Use Map.size: More convenient than Object.keys(obj).length.

  5. Consider performance: Maps are optimized for frequent additions/deletions.