What is JavaScript Map and how to use it ?
In this article, we will discuss Map in JavaScript.
What is Map?
It is an object which holds the key-value pair. It is a collection of elements in which elements are present in key-value pairs. Map remembers the original order of insertions of keys. Key and value in the map could be any object and primitive values. It is provided by ES6. It gives the elements in the same order in which they have been inserted.
Syntax:
const x = new Map( [iterable_objects] );
We can create a new object with the help of the above syntax. It takes parameters as iterable elements which would be stored as key and value pairs.
If we didn’t provide any iterable element to it then it would create an empty object.
Creating an empty object:
const x = new Map( );
Return type: It returns a new map object.
Example of a Map which takes an iterable element as a parameter:
Javascript
<script> const company = new Map([ ["name", "GFG"], ["no_of_employee", 200], ["category","education"] ]); function print(key,values) { console.log(values+"=>"+key); } company.forEach(print);<script/> |
Output:
name=>GFG no_of_employee=>200 category=>education
Example of Map that returns an empty object.
Javascript
<script> const company = new Map(); company.set("name","GFG"); company.set("no_of_employee",200); company.set("category","education"); function print(key,values) { console.log(values+"=>"+key); } company.forEach(print);<script/> |
Output:
name=>GFG no_of_employee=>200 category=>education
Properties:
Map.prototype.size – It returns the number of elements in a Map.
Example:
Javascript
<script> const company = new Map(); company.set("name","GFG"); company.set("no_of_employee",200); company.set("category","education"); console.log(company.size);<script/> |
Output:
3
Methods
JavaScript Map.prototype[@@iterator]() method: It returns a map iterator function, which is entries method by default
Syntax:
My_map[Symbol.iterator]
Return value: It returns a map iterator function.
Example:
Javascript
<script> const company = new Map(); company.set("name","GFG"); company.set("no_of_employee",200); company.set("category","education"); const iterator1 = company[Symbol.iterator](); for(const item of iterator1) { console.log(item); }<script/> |
Output:
Array ["name", "GFG"] Array ["no_of_employee", 200] Array ["category","education"]
JavaScript Map.prototype.clear() Method: This method removes all the elements from the Map object.
Syntax:
clear()
Return type: Undefined
Example:
Javascript
<script> const company = new Map(); company.set("name","GFG"); company.set("no_of_employee",200); company.set("category","education"); company.clear(); console.log(company.size);<script/> |
Output:
0
JavaScript Map.prototype.delete() Method: This method removes the specified element from a Map as a key.
Syntax:
delete(key)
This removes the key from the object.
Return type:
- True, if the element has been removed from the object
- False, if the element doesn’t exist in the object
Example:
Javascript
<script> const company = new Map(); company.set("name","GFG"); company.set("no_of_employee",200); company.set("category","education"); company.delete('category'); console.log(company.size); <script/> |
Output:
2
JavaScript Map.prototype.entries() Method: It returns an iterable object. And that object contains the key-value pair of each element.
Syntax:
entries()
Return type: It returns a new iterable object type.
Example:
Javascript
<script> const company = new Map(); company.set("name","GFG"); company.set("no_of_employee",200); company.set("category","education"); const itr = company.entries(); console.log(itr.next().value);<script/> |
Output:
Array ["name","GFG"]
JavaScript Map.prototype.forEach() Method: It iterates through each element of the object and executes a callback function on each element of the object.
Syntax:
my_map.forEach(callback, ele); my_map.forEach(callback);
Return type: undefined
Example:
Javascript
<script> const company = new Map(); company.set("name","GFG"); company.set("no_of_employee",200); company.set("category","education"); function print(key,value) { console.log(value + "=>" + key); } company.forEach(print);<script/> |
Output:
name=>GFG no_of_employee=>200 category=>education
JavaScript Map.prototype.get() Method: It returns the value of the key.
Syntax:
get(key)
Return type:
- It returns the value if the key is present.
- undefined if the key is not present.
Example:
Javascript
<script> const company = new Map(); company.set("name","GFG"); company.set("no_of_employee",200); company.set("category","education"); console.log( company.get("name") );<script/> |
Output:
GFG
JavaScript Map.prototype.has() Method: It returns a boolean value depending on whether the key is present or not.
Syntax:
has(key)
Return type:
- True, if the key is present
- False, if the key is not present
Example:
Javascript
<script> const company = new Map(); company.set("name","GFG"); company.set("no_of_employee",200); company.set("category","education"); if(company.has("name")) { console.log("It is present"); } else { console.log("It is not present"); }<script/> |
Output:
It is present
JavaScript Map.prototype.keys() Method: It returns an iterable object that contains the key in the same insertion order.
Syntax:
keys()
Return type: It returns an iterable object.
Example:
Javascript
<script> const company = new Map(); company.set("name","GFG"); company.set("no_of_employee",200); company.set("category","education"); const itr = company.keys(); console.log(itr.next().value);<script/> |
Output:
name
JavaScript Map.prototype.set() Method: It is used to add keys and values to map objects.
Syntax:
set(key, value)
key: key of the element to be added into the map
value: the value of the key to be added
Return type: It returns the Map object.
Example:
Javascript
<script> const company = new Map(); company.set("name","GFG"); company.set("no_of_employee",200); company.set("category","education"); console.log(company.get("no_of_employee"));<script/> |
Output:
200
JavaScript Map.prototype.values() Method: It returns the iterable object that contains the values of each element of the map.
Syntax:
values()
Return type: It returns the new Map object.
Example:
Javascript
<script> const company = new Map(); company.set("name","GFG"); company.set("no_of_employee",200); company.set("category","education"); const itr = company.values(); console.log(company.get("category"));<script/> |
Output:
education


Please Login to comment...