- 主题:[求助]js一段代码求助
Array(5).map((x,i)=>i); //[ , , , , ]
Array(5).fill().map((x,i)=>i); //[ 0, 1, 2, 3, 4 ]
为什么这两句结果不同呢?
--
FROM 167.220.232.*
有点意思,这是专门用来考人的吧,谁会写这种无厘头的代码
第一句,是分配了5个空间的数组,但数组里面没有任何东西,所以map不了,因为里面是空的
第二句,fill之后数组里面有个5个undefined元素,所以map之后成了index
--
FROM 115.70.49.*
map calls a provided callback function once for each element in an array, in order, and constructs a new array from the results. callback is invoked only for indexes of the array which have assigned values, including undefined. 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).
【 在 zswolf2000 (少年壮志不言愁) 的大作中提到: 】
: Array(5).map((x,i)=>i); //[ , , , , ]
: Array(5).fill().map((x,i)=>i); //[ 0, 1, 2, 3, 4 ]
: 为什么这两句结果不同呢?
--
FROM 14.152.90.*