Sparse Arrays
Published on August 11, 2023Updated on March 23, 2024
Loading content...
中文 Sparse Arrays 叫作稀疏数组
控制台打印会展示
CODE(8) [empty × 8]
一些老的 API 执行会跳过这些 empty 的数组,新的会把这些 empty 作为 undefined 来执行
以下内容摘自 MDN
Empty slots in sparse arrays behave inconsistently between array methods. Generally, the older methods will skip empty slots, while newer ones treat them as undefined
.
Among methods that iterate through multiple elements, the following do an in
check before accessing the index and do not conflate empty slots with undefined
:
concat()
copyWithin()
every()
filter()
flat()
flatMap()
forEach()
indexOf()
lastIndexOf()
map()
reduce()
reduceRight()
reverse()
slice()
some()
sort()
splice()
For exactly how they treat empty slots, see the page for each method.
These methods treat empty slots as if they are undefined
: