• JavaScript Video Tutorials

JavaScript - Map.values() Method



The Map.values() method in JavaScript doesn't accept any parameters and returns a new iterator object that contains the values for each element present in the Map object, in the insertion order. A Map object is a collection of key-value pairs where each key is unique and can be of any data type, and each value can also be of any data type.

The values() method is compatible in almost every browser such as Chrome, Edge, FireFox, Opera, and Safari.

Syntax

Following is the syntax of JavaScript Map values() method −

values()

Parameters

This method does not accept any parameters.

Return value

This method returns a new Iterator object containing the values for each element in the Map in insertion order.

Examples

Example 1

In the below example, we are iterating through the "values" of each element in the Map object using Map.values() method and printing them in insertion order using for...of loop from the result of the values() method −

<html>
<body>
   <script>
      const map = new Map();
      map.set('a', 'apple');
      map.set('b', 'banana');
      map.set('c', 'cherry');
      
      const Iterator = map.values();
      for (let value of Iterator) {
         document.write(value, "<br>");
      }
   </script>
</body>
</html>

The above program returns a new set iterator object "Iterator" that contains the "values" for each element in this Map in insertion order.

Example 2

In this example, we're using the values() method to create an iterator for the values in the Map. Then, we are manually retrieving each value element using the next() method on the iterator −

<html>
<body>
   <script>
      const map = new Map();
      
      map.set('a', 'apple');
      map.set('b', 'banana');
      map.set('c', 'cherry');
      
      const iterator = map.values();
      document.write(iterator.next().value, "<br>");
      document.write(iterator.next().value, "<br>");
      document.write(iterator.next().value);
   </script>
</body>
</html>

If we execute the above program, it prints each "value" present in the set, in insertion order.

Example 3

Here, the Map object is empty and we are trying to retrieve elements using the next() method on the iterator −

<html>
<body>
   <script>
      const map = new Map();
      const iterator = map.values();
      document.write(iterator.next().value);
   </script>
</body>
</html>

It returns "undefined" as result.

Example 4

In this example, the "iterator.next().done" will return "true", that indicates there are no more values present −

<html>
<body>
   <script>
      const map = new Map();
      map.set('a', 'apple');
      map.set('b', 'banana');
      map.set('c', 'cherry');
      
      const iterator = map.values();
      document.write(iterator.next().value, "<br>");
      document.write(iterator.next().value, "<br>");
      document.write(iterator.next().value, "<br>");
      document.write(iterator.next().done);
   </script>
</body>
</html>

If we execute the above program, it prints each "value" present in the set, in insertion order and returns "true" as all the values from the set are finished.

Advertisements