English 中文(简体)
ES6 - Maps And Sets
  • 时间:2024-12-22

ES6 - Maps and Sets


Previous Page Next Page  

ES6 introduces two new data structures − maps and sets. Let us learn about them in detail.

Maps

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 psted below −

Sr.No Object Map
1 Keys cannot be Object type Keys can be any type
2 Keys are not ordered Keys are ordered
3 not iterable iterable

Syntax

The syntax for Map is given below −


let map = new Map([iterable])
let map = new Map()

Example

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"}

Checking size of the map

The size property can be used to determine the number of values stored in the map.

Syntax

The syntax for checking the size of the map is given below −


map_name.size

Example


<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 −

Sr.No Object & Map
1 set(key,value)

Adds key and value to map

2 get(key)

Returns value if key is matched

3 has(key)

Returns true if an element with the specified key exists; else returns false

4 keys()

Returns an iterator that contains the keys for each element in the map object

5 values()

Returns an iterator that contains the values for each element in the map object

6 entries()

Returns an iterator that contains the key-value pairs for each element in the Map

7 delete(key)

Removes the specified element from a Map object

WeakMap

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.

Syntax

The syntax for WeakMap is stated below −


new WeakMap([iterable])

Example 1


<script>
   let emp = new WeakMap();
   emp.set(10, Sachin );// TypeError as keys should be object
</script>

Example 2


<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}

Set

A set is an unordered collection of unique values. This data structure can contain values of primitive and object types.

Syntax

The syntax for Set is given below −


new Set([iterable])
new Set()

Example


<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"}

Checking the size of a set

The size property of the Set object can be used to query the number of elements in the Set.

Syntax

The syntax for checking the size of a set is mentioned below −


set.size

Example


<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

Iterating a Set

We can use the forEach and for..of loops to iterate through a Set. This is shown in the example below −

Example


<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 −

Sr.No Object & Map
1 add(element)

Adds an element to the Set

2 has(element)

Returns true if element found; else returns false

3 delete(element)

Delete specific element from the Set

4 clear()

Clears all elements from the Set

WeakSet

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 {{…}, {…}}
Advertisements