๐sparse array
JS โฉ object โฉ built-in โฉ Array โฉ sparse array
array that has "holes" in it.
"holes" are (usually) treated as undefined.
loops and array methods treat "holes" in sparse arrays differently.
forEach๏ผignores "holes".
map ๏ผpreserves holes.
filter ๏ผignores holes.
if an array literal contains multiple commas in a row, with no value between, the array is sparse.
array literal syntax allows an optional trailing comma, so
[,,]
has a length of 2, not 3โ
Array(n) vs. Array(n).fill()
Array(3)
ๅชๆ่จญๅฎ้ฃๅ้ทๅบฆ
{length: 3}
๏ผไธฆไธๆ่จญๅฎใๆดๆธ็ดขๅผใๅฑฌๆงๅฆๆๅ
.map()
๏ผๅชๆๅพๅฐ็ฉบ้ฃๅ๏ผๅ ็บ.map()
ๆไฟ็ใๆดใใ
Array(3).fill()
ๆๅกซๅ ฅ
undefined
๏ผไธฆ่จญๅฎใๆดๆธ็ดขๅผใๅฑฌๆง๏ผ้ๆไฝฟ็จ.map()
ๅฐฑๆๆๅฏฆ้ๆๆใ
Last updated