I’m using flat() to count the number of empty or undefined items inside a nested array.
[[undefined, 4], [4]]
Using flat on this array will return 3 items:
- undefined
- 4
- 4
However on this array:
[[], [], []]
using flat will return an empty array.
Is there a way to use it so it returns 3 undefined items?
>Solution :
You have to use flatMap for this reason:
[[], [], []].flatMap(item => item.length ? item : undefined)
returns what you need