- The keys and values in the map collection may be of any type and if a value is added to the map collection using a key which already exists in the collection, then the new value replaces the old value.
- The iteration of elements in a map object is done in the insertion order and a “for…” loop returns an array of all [key, value] pairs for each iteration.
- The keys used in maps can be any type of values such as functions, objects etc whereas the keys in objects are limited to symbols and strings.
- The size of a map can be known easily by using the size property but while dealing with objects, the size has to be determined manually.
- A Map should be preferred in cases where the requirement involves frequent addition and removal of [key, value] pairs because a map is an iterative data type and can be directly iterated whereas iterating an object requires obtaining its keys in a specific manner.
The Map.get() method takes the key of the element to be returned as an argument and returns the element which is associated with the specified key passed as an argument. If the key passed as an argument is not present in the map, then Map.get() method returns undefined.
- key: It is the key of the element of the map which has to be returned.
- The Map.get() method returns the element which is associated with the specified key passed as an argument or undefined if the key passed as an argument is not present in the map.
Examples of the above function are provided below.
Input : var myMap = new Map(); myMap.set(0, 'geeksforgeeks'); document.write(myMap.get(0)); Output: "geeksforgeeks"
Explanation: In this example, a map object “myMap” has been created with a single [key, value] pair and the Map.get() method is used to return the element which is associated with the key ‘0’.
Input : var myMap = new Map(); myMap.set(0, 'geeksforgeeks'); myMap.set(1, 'is an online portal'); myMap.set(2, 'for geeks'); document.write(myMap.get(0),"<br>"); document.write(myMap.get(2),"<br>"); document.write(myMap.get(4),"<br>"); Output: "geeksforgeeks" "for geeks" undefined
Explanation: In this example, a map object “myMap” has been created with a three [key, value] pairs and the Map.get() method is used to return the elements which are associated with the keys ‘0’, ‘2’ and ‘4’. Map.get() returns undefined when key passed is ‘4’ since the key ‘4’ is not present in the map.
"geeksforgeeks" "for geeks" undefined
- If the variable is not of the Map type then the Map.get() operation throws a TypeError.
- If the index specified in the Map.get() function doesn’t belong to the [key, value] pairs of a map, the Map.get() function returns undefined .
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.