Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 627
v-moe Avatar asked Oct 29 '25 14:10

v-moe


2 Answers

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
like image 181
T.J. Crowder Avatar answered Oct 31 '25 12:10

T.J. Crowder


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.

like image 31
VLAZ Avatar answered Oct 31 '25 10:10

VLAZ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!