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
  • 2,960
  • 6
  • 33
  • 71

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
  • 351,820
  • 24
  • 303
  • 358