how array methods deal with "holes" ?
Last updated
Was this helpful?
Last updated
Was this helpful?
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 holes
copyWithin
— holes are copied
entries
, keys
, values
— treats holes as undefined
every
— ignores holes
fill
— fills holes
filter: removes holes (its result is always a "dense" array). arr.filter()
find
— treats holes as elements
findIndex
— treats holes as elements
forEach: ignores holes (closure not invoked). for-of vs. forEach
indexOf
— ignores holes
join
— converts holes to empty strings
lastIndexOf
— ignores holes
map: preserves holes (but ignored by mapping function)
pop
— treat holes as elements
push
— preserves holes
reduce
, reduceRight
— ignores holes
reverse
— preserves holes
shift
— treat holes as undefined
slice
— preserves holes
sort
— preserves holes
toString
— preserves holes
unshift
— preserves holes
values
— converts holes to undefined