jsguides

Map.prototype.entries()

map.entries()

The entries() method returns a new Map Iterator that yields [key, value] pairs in insertion order. Use Map.prototype.entries() when you want both the key and the value while iterating, or when you need to convert a map into an array of tuples.

Syntax

map.entries()

Return Value

Returns an iterator that produces [key, value] arrays for each entry in the map. The iterator object has a next() method that returns {done: false, value: [key, value]} until all entries are exhausted, then returns {done: true}.

Examples

Basic iteration

The for...of loop destructures each [key, value] pair directly in the loop head, giving you named variables for both sides. This is the most common pattern for iterating a Map when you need both the key and the value.

const scores = new Map([
  [alice, 12],
  [bob, 9]
]);

for (const [name, score] of scores.entries()) {
  console.log(`${name}: ${score}`);
}
// alice: 12
// bob: 9

Spreading entries() into an array converts the entire Map into a flat list of tuples. This is useful when you want to inspect the raw structure, serialize the Map, or pass it to a function that expects arrays.

Convert a map to an array

Spreading entries() into an array converts the entire Map into a flat list of tuples. Once in array form, you can serialize the data or pass it to functions that expect plain arrays.

const settings = new Map([
  [theme, dark],
  [language, en]
]);

console.log([...settings.entries()]);
// [[theme, dark], [language, en]]

Calling next() directly gives you step-by-step control over iteration, which is handy when you want to pull values lazily rather than consume the entire iterator at once. Each call advances the internal cursor and returns the next pair with both the key and value.

Manual iteration with next()

const ids = new Map([
  [first, 101],
  [second, 102]
]);

const iterator = ids.entries();

console.log(iterator.next().value);
// [first, 101]

console.log(iterator.next().value);
// [second, 102]

Once you spread entries() into an array, you can chain Array methods like filter(), map(), and reduce(). This pattern is especially useful when you need to transform or subset a Map before working with the results, such as filtering entries by price or sorting by value.

Using with Array methods

const products = new Map([
  [apple, 1.50],
  [banana, 0.75]
]);

const expensive = [...products.entries()]
  .filter(([name, price]) => price > 1.00);

console.log(expensive);
// [[apple, 1.50]]

The entries() method returns a new iterator object that yields [key, value] pairs for each entry in the Map, in insertion order. It completes the trio of iteration methods alongside keys() and values(), and is the go-to choice when you need both sides of each entry at once.

entries() vs for…of with destructuring

The default for...of loop on a Map already yields [key, value] pairs — it calls entries() internally. The two forms are equivalent:

const m = new Map([['a', 1], ['b', 2]]);

// Explicit entries()
for (const [k, v] of m.entries()) {
  console.log(k, v);
}

// Implicit — same result
for (const [k, v] of m) {
  console.log(k, v);
}

Use entries() explicitly when you want to make the intent obvious, or when you need to spread the iterator into an array ([...m.entries()]) for further processing with array methods.

Converting a Map to an object

Object.fromEntries() accepts the iterator that entries() produces, making it the standard way to convert a Map to a plain object:

const settings = new Map([['theme', 'dark'], ['lang', 'en']]);
const obj = Object.fromEntries(settings.entries());
// or equivalently:
const obj2 = Object.fromEntries(settings);
console.log(obj); // { theme: 'dark', lang: 'en' }

Note that Map keys can be any type, but plain objects only support string (and symbol) keys. Object values with non-string keys will have their keys coerced to strings.

Insertion order

entries() yields pairs in insertion order, consistent with keys() and values(). This order is guaranteed by the ES6 Map specification and preserved across all operations except clear() and re-insertion.

Iterator exhaustion

Like all Map iterators, the one returned by entries() is a one-time-use object. Once all pairs have been yielded, subsequent calls to next() return { done: true }. Spreading the iterator a second time produces an empty array. Call entries() again to get a fresh iterator if you need to iterate the same Map more than once.

const m = new Map([['a', 1], ['b', 2]]);
const iter = m.entries();
console.log([...iter]); // [['a', 1], ['b', 2]]
console.log([...iter]); // [] — iterator is exhausted

See Also