> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/web/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/web/js/val/builtin/arr/sparse/holes.md).

# how array methods deal with "holes" ?

[JS](/web/js.md) ⟩ [object](/web/js/val/obj.md) ⟩ [built-in](/web/js/val/builtin.md) ⟩ [Array](/web/js/val/builtin/arr.md) ⟩ [sparse array](/web/js/val/builtin/arr/sparse.md) ⟩ how array methods deal with "holes" ?&#x20;

{% tabs %}
{% tab title="👥 相關" %}

* [arr.removeValue()](/web/js/val/builtin/arr/ext/arr.removevalue.md)
* [arr.removeUndefined()](/web/js/val/builtin/arr/ext/arr.removeundefined.md)
* [Array(n) vs. Array(n).fill()](/web/js/val/builtin/arr/static-methods/fill.md)
  {% endtab %}

{% tab title="📗 參考" %}

* [ ] Better Programming ⟩ [What Are Holes in Arrays?](https://betterprogramming.pub/what-are-holes-in-arrays-3ac5fcbcd1c)
* [ ] [JavaScript: The Definitive Guide](/web/master/ref/javascript-the-definitive-guide.md) ⟩&#x20;
  * [ ] 7.1.1 Array Literals
  * [ ] 7.3 Sparse Arrays
    {% endtab %}

{% tab title="📘 手冊" %}

* [Array.prototype method](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)
  {% endtab %}
  {% endtabs %}

{% hint style="warning" %}
[**loops**](/web/js/grammar/statement/loop.md) and [array methods](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) <mark style="color:yellow;">**treat**</mark> "<mark style="color:yellow;">**holes**</mark>" in <mark style="color:purple;">**sparse arrays**</mark> <mark style="color:red;">**differently**</mark>.

* [**for-of**](/web/js/grammar/statement/loop/for/of.md)：<mark style="color:yellow;">**treats**</mark> "<mark style="color:yellow;">**holes**</mark>" as [**undefined**](/web/js/val/prim/undefined.md).
* `concat` — keeps holes
* `copyWithin` — holes are copied
* `entries`, `keys`, `values` — treats holes as `undefined`
* `every` — ignores holes
* `fill` — fills holes
* [**filter**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)： <mark style="color:red;">**removes**</mark> holes (its result is <mark style="color:yellow;">**always a "dense" array**</mark>).  :point\_right: [arr.filter()](/web/js/val/builtin/arr/method/arr.filter.md)
* `find` — treats holes as elements
* `findIndex` — treats holes as elements
* [**flat**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat) - <mark style="color:red;">**removes**</mark> <mark style="color:yellow;">**holes**</mark> <mark style="color:red;">**but**</mark> <mark style="color:orange;">**keeps**</mark> [**undefined**](/web/js/val/prim/undefined.md).&#x20;
* [**forEach**](/web/js/grammar/statement/loop/for/foreach.md)： <mark style="color:red;">**ignores**</mark> holes (closure not invoked).  :point\_right: [for-of vs. forEach](/web/js/grammar/statement/loop/for/of/vs-foreach.md)
* `indexOf` — ignores holes
* `join` — converts holes to empty strings
* `lastIndexOf` — ignores holes
* [**map**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map)： <mark style="color:yellow;">**preserves**</mark> holes (but <mark style="color:red;">**ignored**</mark> 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`
  {% endhint %}
