- ES6 - Discussion
- ES6 - Useful Resources
- ES6 - Quick Guide
- ES9 - New Features
- ES8 - New Features
- ES7 - New Features
- ES6 - Browsers
- ES6 - Image Map
- ES6 - Debugging
- ES6 - Multimedia
- ES6 - Animation
- ES6 - Validations
- ES6 - Proxy API
- ES6 - Reflect API
- ES6 - Object Extensions
- ES6 - Error Handling
- ES6 - Modules
- ES6 - Promises
- ES6 - Maps And Sets
- ES6 - Classes
- ES6 - Collections
- ES6 - Iterator
- ES6 - HTML DOM
- ES6 - RegExp
- ES6 - Math
- ES6 - Date
- ES6 - Arrays
- ES6 - New String Methods
- ES6 - Symbol
- ES6 - Strings
- ES6 - Boolean
- ES6 - Number
- ES6 - Objects
- ES6 - Page Printing
- ES6 - Void Keyword
- ES6 - Dialog Boxes
- ES6 - Page Redirect
- ES6 - Cookies
- ES6 - Events
- ES6 - Functions
- ES6 - Loops
- ES6 - Decision Making
- ES6 - Operators
- ES6 - Variables
- ES6 - Syntax
- ES6 - Environment
- ES6 - Overview
- ES6 - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
ES6 - Maps and Sets
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 |
Adds key and value to map |
2 |
Returns value if key is matched |
3 |
Returns true if an element with the specified key exists; else returns false |
4 |
Returns an iterator that contains the keys for each element in the map object |
5 |
Returns an iterator that contains the values for each element in the map object |
6 |
Returns an iterator that contains the key-value pairs for each element in the Map |
7 |
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 |
Adds an element to the Set |
2 |
Returns true if element found; else returns false |
3 |
Delete specific element from the Set |
4 |
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