Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

222
Vistas
Is there a difference between iterating a Map directly and via an iterator returned by entries()?

If I have a Map in JavaScript, e.g

const myMap = new Map()
myMap.set(0, 'zero')
myMap.set(1, 'one')

Then both of these two seem to be valid to iterate through the key-value pairs (and many other options which I am currently not interested in):

// with .entries()
for (const [key, value] of myMap.entries()) {
  //
}
// without
for (const [key, value] of myMap) {
  //
}

Is there any edge case where they do not do the same?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Is there any edge case where they do not do the same?

No. The iterator object you get from entries is exactly the same one provided by the map itself. In fact, someMap[Symbol.iterator] (the function called when you ask an object for its iterator) is literally the exact same function as someMap.entries:

const someMap = new Map();
console.log(someMap[Symbol.iterator] === someMap.entries);

In the spec:

  • Map.prototype.entries
  • Map.prototype[@@iterator] (@@iterator is Symbol.iterator) — which literally just points to entries
about 4 years ago · Juan Pablo Isaza Denunciar

0

Both do exactly the same by default.

When for...of is used with a value as for (const item of collection) then the well-known symbol @@iterator is used to find what the values would be.

For Map objects, @@iterator will return .entries(). It's exactly like calling the method because it is:

const map = new Map();

console.log(map[Symbol.iterator] === map.entries);

Therefore, both loops will do the same.

Small caveat: that is by default. If the @@iterator symbol is overriden, then there can be different results:

const myMap = new Map()
myMap.set(0, 'zero')
myMap.set(1, 'one')

myMap[Symbol.iterator] = function* () {
  const entries = this.entries();
  for (const [key, value] of entries)
    yield [key, value.toUpperCase()]; //transform the value
}

// with .entries()
for (const [key, value] of myMap.entries()) {
  console.log("with .entries()", key, value)
}
// without
for (const [key, value] of myMap) {
  console.log("without", key, value)
}

However, that is exceptionally unusual to happen. And if it does then it is likely that whoever supplied the map might actually want a different iteration logic.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda