Why Array(100).map((_, I) => I + 1) Doesn't Return [1, 2, ..., 100]?
I found it strange that Array(100).map(function (_, i) { return i + 1; }) returns [undefined, undefined, ... , undefined] rather than [1, 2, ..., 100], i. e. the mapping not happe
Solution 1:
From the Array.prototype.map
reference:
"callback is invoked only for indexes of the array which have assigned values; it is not invoked for indexes that are undefined, those which have been deleted or which have never been assigned values."
The array that you create with Array(100)
has a length of 100, but there are not items in it. An array containing 100 items that are undefined
on the other hand has 100 items that have a value (that is the value undefined
), so the callback will be called for each of them.
Post a Comment for "Why Array(100).map((_, I) => I + 1) Doesn't Return [1, 2, ..., 100]?"