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