Emilio Emilio - 5 months ago 16
Javascript Question

How to check if there is at least one element in a multidimensional array? (Silly Basic Javascript Questionn)

How to check if there is at least one element in an array? (I want to verify if the array is not empty.)

Sorry for this silly question, I already spent too much time on Google... (Google only returns complex situations and solutions. It seems like my question is too simple.).

EDIT: Ok, but what will happen with an array like [,,[],,[],,,] ? For my purposes, it should be considered as empty.

EDIT 2: Sorry, guys, for the confusion! At first, I didn't even know exactly what I was looking for. Thank you all!


This would fulfill your new requirements:

function isEmpty(arr) {
    if (!Array.isArray(arr)) {
        return false;

    return arr.every(isEmpty);

What it does: with the help of Array.prototype.every it checks that the every item left is an empty array. And the array holes are automatically skipped by .every().