map.entries() returns an iterator of [key, value] arrays, one pair per entry, in the order the keys were added. The default iterator of a Map is the same as entries(), so for (const [k, v] of map) and for (const [k, v] of map.entries()) do the same work. Keys can be any value (SameValueZero).
Remember
map.entries() → iterator of [key, value]
for (const [k, v] of map) → same as entries()
for (const [k, v] of map.entries()) → explicit
Object.fromEntries(map) → plain object (string keys)
[...map.entries()] → array of pairs
Syntax
Call it on a Map. It takes no arguments and is usually used with for...of and destructuring.
JavaScript
map.entries()
Call
What it does
map.entries()
An iterator of [key, value] pairs.
for (const [k, v] of map)
The short form: a Map is iterable via entries by default.
Object.fromEntries(map)
Build a plain object from the pairs (keys become strings).
Built into JavaScript since ES2015. For values alone, see values(). For keys alone, see keys().
Library declarations
JavaScript
map.entries() // returns a Map Iterator of [key, value]
// no parameters
// default @@iterator of Map is entries
Parameters
Parameter
Description
(none)
This method takes no parameters. Call it as map.entries().
Return Value
Return Value
Description
Map Iterator
An iterator where each step yields a two-element array [key, value], in insertion order.
JavaScript Examples
Example 1 — Loop Over Pairs with Destructuring
Print every key and value with for...of and array destructuring.
JavaScript
const ages = new Map();
ages.set("Ann", 31);
ages.set("Bob", 27);
for (const [name, age] of ages.entries()) {
console.log(name + ": " + age);
}