Array.map doesn't seem to work on uninitialized arrays

后端 未结 8 1963
忘了有多久
忘了有多久 2020-12-03 10:35

I\'m trying to set default values on an uninitialized array using the map function but it doesn\'t seem to work, any ideas on how to set default values?

Consider thi

8条回答
  •  不知归路
    2020-12-03 10:57

    With .map(), undefined elements are skipped and not passed to the callback so if you have an array with no elements that actually contain anything, then the callback is never called.

    From the ECMA script 262, 5.1 specification, section 15.4.4.19:

    callbackfn is called only for elements of the array which actually exist; it is not called for missing elements of the array.

提交回复
热议问题