
A Map is a collection of keyπβββvalue pairs, similar to an object. It stores the keyπβββvalue pairs in the insertion order.
We can create a Map by passing an iterable object whose elements are in the form of keyπβββvalue (Eg. [ [1,βoneβ], [2,βtwoβ] ] ), or you can create an empty Map, then insert entries.
Why we need Map if we have ObjectΒ s
- A Map is similar to Object, but any value can be used as key in maps, but keys in objects are only Strings and Symbols

2. The keyπβββvalue pairs in Map maintains the insertion order, whereas Object donβt.
3. You can get the size of the Map, but we donβt have a built-in method to get size in Object (but we can do it manually).
4. Maps are iterable, whereas an Object is not iterable by-default.
5. Maps have additional methods not available to normal objects
Methods inΒ Maps

Example:
- Different Ways to create maps

2. Map methods
Letβs create simple Map:
// creating a map
var fruits = new Map();
fruits.set('π', "Apple");
fruits.set('π', "Mango");
fruits; //{"π" => "Apple", "π" => "Mango"}
Map has a property size which returns the current Map size (total number of entries).
// size
fruits.size; // 2
set(π, value) β Set the value for the key in the Map and returns the Map. If the key is already present in the map, then the value is overwritten.
// set(key , value) method
fruits.set( 'π', "Grapes");
fruits; // {"π" => "Apple", "π" => "Mango", "π" => "Grapes"}
get(π) β Returns the value associated to the key, or returns undefined if there is no key on the Map that matches the passed key.
// get(key)
fruits.get('π'); // GRAPES.
fruits.get('π'); //undefined.
delete(π) β Delete the entry matching the key and returns true. If the key is not found in the map, then it returns false.
// delete(key)
fruits.delete('π') //false
fruits.delete('π'); // true
has(π) β Returns true if the map has the key provided, else return false.
//HAS(key)
fruits.has('π'); // true
fruits.has('π'); //false
keys() β Returns a new iterator that contains the keys in insertion order.
//keys
fruits.keys() // MapIterator {"π", "π"}
values() β Returns a new iterator that contains the values in insertion order.
// values
fruits.values() // MapIterator {"Apple", "Mango"}
entries() β Returns an iterator which has an array of [π-value] pair in insertion order.
//entries --> returns key value pair
fruits.entries() // MapIterator {"π" => "Apple", "π" => "Mango"}
clear() β Delete all keyπ-value pairs from the Map.
// clear
fruits.clear(); // deletes all element
fruits; // Map(0) {}
Things you should know aboutΒ Map
You can use any value (object or function or primitive ) as a key in the MapΒ . Map uses the SameValueZero algorithm. It is similar to strict equality (===) but the difference is that NaN is considered equal to NaN (i.e, NaN === NaN β true. In normal JS, this is not true. In this case NaN can be used as key and we donβt have any duplicate NaN).
var map = new Map()
var obj = {};
var arr = [];
var nan = NaN;
var fun = function() {}
map.set(obj, "object");
map.set(arr , "array");
map.set(fun, "function");
map.set(nan, "Wrong number");
map.keys(); // MapIterator {{β¦}, Array(0), Ζ, NaN}
map.values(); // MapIterator {"object", "array", "function","Wrong number"}
Iterating throughΒ Map
Using forβ¦of
// creating a map
var fruits = new Map();
fruits.set('π', "Apple");
fruits.set('π', "Mango");
fruits; //{"π" => "Apple", "π" => "Mango"}
--------------------------------------------------------------------
//Method 1:
for(var [key, val] of fruits) {
console.log(key, value);
}
π Apple
π Mango
--------------------------------------------------------------------
Method 4 --> using entries
for (var [key, val] of fruits.entries()) {
console.log(key , val);
}
π Apple
π Mango
--------------------------------------------------------------------
// Method 2 --> using keys
for(var key of fruits.keys()) {
console.log(key, fruits.get(key));
}
π Apple
π Mango
--------------------------------------------------------------------
Method 3 --> using values
for(var val of fruits.values()) {
console.log(val);
}
Apple
Mango
If you find this helpful surprise π me here.
Share if you feel happy.
Follow Javascript Jeepπ if you feel worthy.