我尝试了这个
> Array(3)
[ <3 empty items> ]
> // and this joins up all the nothings
> Array(3).join('-')
'--'
> // but...
> Array(3).map((x) => 'a')
[ <3 empty items> ]
> // map doesn't work with the array of empty items!
我以为我的结果会跟这个一样
> [undefined, undefined, undefined].map((x) => 'a')
[ 'a', 'a', 'a' ]
那是怎么了?
.map()
方法(以及类似的其他方法)会跳过未初始化的数组位置。 - PointyArray(3).fill(null).map(x => 'a')
。 - Pointy