In my node.js 6.10
app, I am trying to identify in my array looks like this:
[ [ [] ], [] ]
This nesting can go onto n level, and can have elements in arrays at any level. How can I do this? Thanks
P.S. I know I can do it using a n level for loop, but was wondering about a more optimized solution.
Advertisement
Answer
An one-liner:
let isEmpty = a => Array.isArray(a) && a.every(isEmpty); // let zz = [ [ [] ], [], [[[[[[]]]]]] ] console.log(isEmpty(zz))
If you’re wondering how this works, remember that any statement about an empty set is true (“vacuous truth”), therefore a.every(isEmpty)
is true for both empty arrays and arrays that contain only empty arrays.