ES6 - Maps and Sets

ES6 introduces two new data structures − maps and sets. Let us learn about them in detail.
A map is an ordered collection of key-value pairs. Maps are similar to objects. However, there are some differences between maps and objects. These are listed below −
The syntax for Map is given below −
let map = new Map([iterable])
let map = new Map()

The following example creates a map using an iterable constructor −
<script>
   let andy = {ename:"Andrel"},
      varun = {ename:"Varun"},
      prijin = {ename:"Prijin"}
   let empJobs = new Map([
   [andy,'Software Architect'],
   [varun,'Developer']]
   );
   console.log(empJobs)
</script>

The output of the above code is as shown below −
{{...} => "Software Architect", {...} => "Developer"}

The size property can be used to determine the number of values stored in the map.
The syntax for checking the size of the map is given below −
map_name.size

<script>
   let daysMap = new Map();
   daysMap.set('1', 'Monday');
   daysMap.set('2', 'Tuesday');
   daysMap.set('3', 'Wednesday');
   console.log(daysMap.size);
</script>

The output of the above code is as shown below −
3

Following are some common methods that can be used to manipulate maps −
Adds key and value to map
Returns value if key is matched
Returns true if an element with the specified key exists; else returns false
Returns an iterator that contains the keys for each element in the map object
Returns an iterator that contains the values for each element in the map object
Returns an iterator that contains the key-value pairs for each element in the Map
Removes the specified element from a Map object
WeakMap is a small subset of map. Keys are weakly referenced, so it can be non-primitive only. If there are no reference to the object keys, it will be subject to garbage collection.
not iterable
every key is object type
The WeakMap will allow garbage collection if the key has no reference.
The syntax for WeakMap is stated below −
new WeakMap([iterable])

<script>
   let emp = new WeakMap();
   emp.set(10,'Sachin');// TypeError as keys should be object
</script>
<script>
   let empMap = new WeakMap();
   // emp.set(10,'Sachin');// Error as keys should be object
   let e1= {ename:'Kiran'},
      e2 = {ename:'Kannan'},
      e3 = {ename:'Mohtashim'}

   empMap.set(e1,1001);
   empMap.set(e2,1002);
   empMap.set(e3,1003);

   console.log(empMap)
   console.log(empMap.get(e2))
   console.log(empMap.has(e2))
   empMap.delete(e1)
   console.log(empMap)
</script>
The output of the above code is as mentioned below −
{{...} => 1002, {...} => 1003, {...} => 1001}
1002
true
{{...} => 1002, {...} => 1003}

A set is an unordered collection of unique values. This data structure can contain values of primitive and object types.
The syntax for Set is given below −
new Set([iterable])
new Set()

<script>
   let names = new Set(['A','B','C','D']);
   console.log(names)
</script>
The output of the above code is as given below −
{"A", "B", "C", "D"}

The size property of the Set object can be used to query the number of elements in the Set.
The syntax for checking the size of a set is mentioned below −
set.size

<script>
   let names = new Set(['A','B','C','D']);
   console.log(names.size)
</script>
The output of the above code is as given below −
4

We can use the forEach and for..of loops to iterate through a Set. This is shown in the example below −
<script>
   let names= new Set(['A','B','C','D']);
   //iterate using forEach
   console.log('forEach')
   names.forEach(n=>console.log(n))
   
   console.log('for of..')
   
   //iterate using for..of
   for(let n of names){
      console.log(n)
   }
</script>  
The output of the above code is as mentioned below −
forEach
A
B
C
D
for of..
A
B
C
D

The following methods can be used to manipulate a set −
Adds an element to the Set
Returns true if element found; else returns false
Delete specific element from the Set
Clears all elements from the Set
A Weakset holds objects weakly, that means object stored in a WeakSet are subject to garbage collection, if they are not referenced. WeakSets are not iterable and do not have the get method.
<script>

   let e1 = {ename:'A'}
   let e2 ={ename:'B'}
   let e3 ={ename:'C'}

   let emps = new WeakSet();
   emps.add(e1);
   emps.add(e2)
   .add(e3);

   console.log(emps)
   console.log(emps.has(e1))
   emps.delete(e1);
   console.log(emps)
</script>
The output of the above code will be as mentioned below −
WeakSet {{...}, {...}, {...}}
true
WeakSet {{...}, {...}}


    32 Lectures 
     3.5 hours 

 Sharad Kumar

    40 Lectures 
     5 hours 

 Richa Maheshwari

    16 Lectures 
     1 hours 

 Anadi Sharma

    50 Lectures 
     6.5 hours 

 Gowthami Swarna

    14 Lectures 
     1 hours 

 Deepti Trivedi

    31 Lectures 
     1.5 hours 

 Shweta
 Print
 Add Notes
 Bookmark this page