JS: Map Object Tutorial

By Xah Lee. Date: . Last updated: .

New in JS2015.

What is Map Object

The map object is designed to be used as a map data structure (similar to Python Dictionary or Ruby Hash Table or Java Map. ).

Create Map

new Map()
Create a empty map.
new Map(iterable)
Create a map from values in iterable object iterable.

[see Iterable Object]

const xx = new Map();

// add new item
xx.set(1, "n1");
xx.set(2, "n2");

console.log(xx);
// Map { 1 => "n1", 2 => "n2" }

Parent of Map Objects

The parent of any map object is Map.prototype.

Add, Modify Entry

Delete Entry

Check Key Existence

Get Key's Value

Size of Map

Clear Map

Iterate Map

Get All Keys

Get All Values

Add Arbitrary Properties to Map

Because map is a object, you can add object properties to it, but you shouldn't do it.

const xx = new Map([[3, "n3"], [4, "n4"], [5, "n5"]]);

// you can add arbitrary properties to a map object
xx.pp = 22;
console.log(xx.pp === 22);

JavaScript, Map Object

BUY Ξ£JS JavaScript in Depth