JS: Map Constructor

By Xah Lee. Date: .

New in JS2015.

new Map()
Create a empty map.
new Map(iterable)
Create a map from values in Iterable Object.
const m = new Map();

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

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

Example of array to map:

// using nested array as literal expression for map
const m = new Map([ [3, "n3"], [4, "n4"]]);
console.log(m); // Map { 3 => 'n3', 4 => 'n4' }

Any Iterable Object can be used as argument.

Convert Object To/From Map

Convert Object to Map 🚀

JavaScript, Map Object