🔰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