0

Subject pretty clear. Array(4) instantiates a 4-element array, so running map that yields the string "foo" should result in an array with four "foo" strings in it.

But it doesn't. In NodeJS it yields <4 empty items>. Why?

I get that empty is not the same as undefined (as per JS documentation on the Array constructor), but why isn't it iterating the way I expect?

user1713450
  • 1,307
  • 7
  • 18
  • 2
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map#Description *"It is not called for missing elements of the array (that is, indexes that have never been set, which have been deleted or which have never been assigned a value)."* – Kaiido Dec 04 '18 at 05:45
  • `[...Array(4).keys()]` will get you an array of indexes, `Array(4).fill('foo')` will get you all foo's and `[...Array(4)].map((undefined_item,index,all)=>code_here)` will get you anything that the map function returns. – HMR Dec 04 '18 at 07:48

0 Answers0