title: JavaScript arrays: how do I compact excess length?
body: <p>This might look like a duplicate question, but please don't assume this just because it looks similar to others, and be careful with easy answers unless you really understand the ramifications.</p>
  <p>Here is a console dialog to demonstrate the problem:</p>
  <pre><code>&gt; a = [0,1,2]
  &lt; &gt;(3) [0,1,2]
  &gt; a.length
  &lt; 3
  &gt; a[99] = 99
  &lt; 99
  &gt; a.length
  &lt; 100
  &gt; delete a[99]
  &lt; true
  &gt; a.length
  &lt; 99
  </code></pre>
  <p>So this length property of arrays doesn't seem to follow any reasonable specification. Trailing sparse elements should be compacted out, but aren't.</p>
  <p>I think as a result, the length property should never be used in code since it is semantically meaningless. (Instead <code>findLastIndex(() =&gt; true)</code> should be used anywhere a naive programmer woul previously use <code>length</code>.)</p>
  <p>I know how to use the elegant forEach, some, every, find, filter, map, and reduce functions. And I know that filter doesn't preserve sparseness. I can use reduce to do a map which can drop elements or a filter which preserves sparseness.</p>
  <p><strong>How can we reset the array length property to something reasonable without copying the array?</strong></p>
  <p>There may be no answer at all, or rather, the answer may be &quot;there is no way and the array length property is essentially meaningless.&quot;</p>