Open In App

JavaScript Map values() Method

Last Updated : 12 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The JavaScript map.values() method is used to return a new Iterator object that contains the value of each element present in Map. The order of the values are in the same order that they were inserted into the map.

Syntax:

myMap.values()

Parameters:

  • This method does not accept any parameters.

Example 1:

<script>
  let myMap = new Map();

  // Adding key value pair with chaining
  myMap.set(1, "India");
  myMap.set(2, "England");
  myMap.set(3, "Canada");

  // Creating a Iterator object
  const mapIterator = myMap.values();

  // Getting values with iterator
  console.log(mapIterator.next().value);
  console.log(mapIterator.next().value);
  console.log(mapIterator.next().value);
</script>

Output :

India
England
Canada

Example 2:

<script>
  let myMap = new Map();

  // Adding key value pair with chaining
  myMap.set(1, "India");
  myMap.set(2, "England");
  myMap.set(3, "Canada");
  myMap.set(4, "Russia");

  // Creating a Iterator object
  const mapIterator = myMap.values();

  // Getting values with iterator
  let i = 0;
  while (i < myMap.size) {
    console.log(mapIterator.next().value);
    i++;
  }
</script>

Output:

India
England
Canada
Russia

Supported Browsers:

  • Chrome 38
  • Edge 12
  • Firefox 19
  • Opera 25
  • Safari 8

JavaScript Map values() Method – FAQs

What does the values() method do in JavaScript Map?

The values() method in JavaScript Map returns a new iterator object that contains the values for each element in the Map object in insertion order.

Performance considerations with values()?

The values() method has a time complexity of O(1), making it very efficient for accessing the values of a Map.

When should values() be used?

values() is useful when you need to iterate through or perform operations specifically on the values of a Map, such as checking, manipulating, or processing the stored values.

What are some practical applications of the values() method with Map?

Practical applications of the values() method include iterating over values to perform value-based operations, checking for the existence of specific values, and transforming map values into other data structures.

What is the most common use of the values() method?

Most Common Use Cases:

  • Retrieving all values from a Map for processing or displaying them.
  • Checking for the existence of specific values in a Map.
  • Implementing data manipulation or transformation based on values stored in a Map.

Next Article

Similar Reads

three90RightbarBannerImg