JavaScript Map values() Method
Last Updated :
08 Mar, 2021
The 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.
The examples below illustrate the values() method:
Example 1:
Javascript
<script>
let myMap = new Map();
myMap.set(1, "India" );
myMap.set(2, "England" );
myMap.set(3, "Canada" );
const mapIterator = myMap.values();
console.log(mapIterator.next().value);
console.log(mapIterator.next().value);
console.log(mapIterator.next().value);
</script>
|
Output :
India
England
Canada
Example 2:
Javascript
<script>
let myMap = new Map();
myMap.set(1, "India" );
myMap.set(2, "England" );
myMap.set(3, "Canada" );
myMap.set(4, "Russia" );
const mapIterator = myMap.values();
let i = 0;
while (i < myMap.size) {
console.log(mapIterator.next().value);
i++;
}
</script>
|
Output :
India
England
Canada
Russia
Share your thoughts in the comments
Please Login to comment...