0

Empty items are not falsey values; they are empty slots in an array.

> const someEmptyItems = ["index0",,,"index3"];
undefined

> someEmptyItems;
[ 'index0', <2 empty items>, 'index3' ]

> someEmptyItems[1];
undefined

> someEmptyItems.length;
4

Logging the array helps spot the empty array items nanually.

  1. How can I detect empty array items programmatically?

  2. How can I programmatically remove empty items from an array without mutating other (not-empty) values?

Sean D
  • 3,810
  • 11
  • 45
  • 90

1 Answers1

1

You could filter the array and return true for every visited element.

const
    sparseArray = ["index0",,,"index3"],
    notSparseArray = sparseArray.filter(_ => true);

console.log(notSparseArray);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392