Map and Set

Array Map set isa a complex data structures.But that’s not enough for real life. That’s why Map and Set also exist.

Map is a collection of keyed data items, just like an Object. But the main difference is that Map allows keys of any type.

Methods and properties are:

  • new Map() – creates the map.
  • map.set(key, value) – stores the value by the key.
  • map.get(key) – returns the value by the key, undefined if key doesn’t exist in map.
  • map.has(key) – returns true if the key exists, false otherwise.
  • map.delete(key) – removes the value by the key.
  • map.clear() – removes everything from the map.
  • map.size – returns the current element count.

// map let myMap= new Map();

myMap.set('hello',12000);

myMap.set(0,'Lipa Moni');

myMap.set(100,'Lipa Moni');

myMap.delete(100); //map 100 is deleted so map value 2 is now undifined

console.log('myMap Size is '+ myMap.size); // map size is 1 because one map deleted

console.log('Map value '+ myMap.get('hello'));

console.log('Map value 2' + myMap.get(100)); // undefine

console.log('Map size is ' + myMap.size);

show more at https://javascript.info/map-set