forEach on a 'new Array' isn't doing what I expect

前端 未结 4 548
礼貌的吻别
礼貌的吻别 2021-01-18 10:07

I\'m just learning how to use JS higher-order functions (map, forEach, reduce, etc), and have stumbled into confusion. I\'m trying to write a simple \'range\' function, but

4条回答
  •  没有蜡笔的小新
    2021-01-18 10:25

    The array is defined with 4 entires each of which is undefined.

    Map will not iterate over undefined entires, it skips them.

    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.

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

提交回复
热议问题