๐ฐhow array methods deal with "holes" ?
JS โฉ object โฉ built-in โฉ Array โฉ sparse array โฉ how array methods deal with "holes" ?
loops and array methods treat "holes" in sparse arrays differently.
concat
โ keeps holescopyWithin
โ holes are copiedentries
,keys
,values
โ treats holes asundefined
every
โ ignores holesfill
โ fills holesfilter๏ผ removes holes (its result is always a "dense" array). ๐ arr.filter()
find
โ treats holes as elementsfindIndex
โ treats holes as elementsforEach๏ผ ignores holes (closure not invoked). ๐ for-of vs. forEach
indexOf
โ ignores holesjoin
โ converts holes to empty stringslastIndexOf
โ ignores holesmap๏ผ preserves holes (but ignored by mapping function)
pop
โ treat holes as elementspush
โ preserves holesreduce
,reduceRight
โ ignores holesreverse
โ preserves holesshift
โ treat holes asundefined
slice
โ preserves holessort
โ preserves holestoString
โ preserves holesunshift
โ preserves holesvalues
โ converts holes toundefined
Last updated