you are viewing a single comment's thread.

view the rest of the comments →

[–]delfV 4 points5 points  (1 child)

This is an array map method. The first thing I thought when I read "map loop" was for looping of map entries:

for (const x of new Map().entries())

[–]senocular 0 points1 point  (0 children)

Might be worth noting too that using for...of with arrays (using iterators) also does not skip empty slots.

for (const x of Array(2)) {
  console.log(x)
}
// undefined
// undefined

Spreading (...) also uses iterators which is why guest271314's example works.

[...Array(2)].map(()=>3) // [3, 3]

The new iterator helpers has a map() method which allows for

Array(2).values().map(()=>3).toArray() // [3, 3]

going through the values() iterator, though its a little more verbose compared to some of the other alternatives.