The values() method of the Map object in JavaScript returns a new Iterator object that contains the values for each element in the Map object in insertion order.
The syntax of Map.values() method is:
values()
This values() method of Map returns a new Iterator object that contains the values for each element in the Map object in insertion order.
Map.values() returns value of type Iterator
.
In JavaScript, we can use the values()
method to get an iterator object and use it to iterate over the values of a Map.
For example,
map
with initial key-value pairs.values()
method to get an iterator object iterator
for the map
.for...of
loop to iterate over the iterator
and log each value to the console using console.log()
.const map = new Map([['a', 1], ['b', 2], ['c', 3]]);
const iterator = map.values();
for (const value of iterator) {
console.log(value);
}
1 2 3
In JavaScript, we can use the values()
method to convert a Map object to an array of values.
For example,
map
with initial key-value pairs.values()
method to get an iterator object iterator
for the map
.iterator
to an array using the Array.from()
method and store it in the variable arrayOfValues
.arrayOfValues
to the console using console.log()
.const map = new Map([['a', 1], ['b', 2], ['c', 3]]);
const iterator = map.values();
const arrayOfValues = Array.from(iterator);
console.log(arrayOfValues);
[ 1, 2, 3 ]
In JavaScript, we can use the values()
method to iterate over a Map object containing objects as values.
For example,
map
with initial key-value pairs where values are objects.values()
method to get an iterator object iterator
for the map
.for...of
loop to iterate over the iterator
and log each value to the console using console.log()
.const map = new Map([
['person1', { name: 'John' }],
['person2', { name: 'Jane' }]
]);
const iterator = map.values();
for (const value of iterator) {
console.log(value);
}
{ name: 'John' } { name: 'Jane' }
In this JavaScript tutorial, we learned about values() method of Map: the syntax and few working examples with output and detailed explanation for each example.