๐Ÿ”ฐ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.

  • for-of๏ผštreats "holes" as undefined.

  • 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

  • flat - removes holes but keeps undefined.

  • 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

Last updated