4

How do I check whether a nested array is empty or not? The array looks like this when there is no data:

const data = [ [  ] ]

And if it has data it looks like this:

const data = [
  [{"Product": 7  }]
]

To check if it is empty I am doing the following which does not work:

if (!Array.isArray(data[0][0]) || data[0][0].length === 0) {
  return "Data is empty"
}
  1. What I don't understand is why !Array.isArray(data[0][0]) returns true, which means the nested array is not an array (which is strange because it is an array, just empty). According to the docs, Array.isArray([]) returns true so how can !Array.isArray(data[0][0] return true?

  2. The data[0][0].length part throws an error saying "TypeError: "data[0][0] is undefined"". Why would that be the case?

  3. That leads to the question of how to check if a nested array is empty or not?

5
  • 3
    it should be data[0].length Commented May 11, 2020 at 23:08
  • 3
    You only have one level of nesting but you're trying to do two. Just change it to data[0] instead. Commented May 11, 2020 at 23:09
  • so yeah just remove the extra [0] Commented May 11, 2020 at 23:09
  • data[0][0] is undefined (data[0] is an empty array), which indeed isn't an array Commented May 11, 2020 at 23:11
  • Looks like a simple oversight. data is the parent array. It has one item in it which is the child array. So to access the first item of data (the child array) you have to do data[0]. I went one level too deep! Commented May 12, 2020 at 10:29

4 Answers 4

6

You could do something like this:

function checkIfEmpty(array) {
  return Array.isArray(array) && (array.length == 0 || array.every(checkIfEmpty));
}


console.log(checkIfEmpty([[{"name":"something"}]])); // false
console.log(checkIfEmpty([[]])); // true
Sign up to request clarification or add additional context in comments.

Comments

3

data[0][0] is the first element of nested array: for first example it is undefined, for second object {"Product": 7 }. You need to check data[0] - it is first nested array, so you need the following:

if (!Array.isArray(data[0]) || data[0].length === 0) {
    return "Data is empty"
}

Comments

1

You can check whether an array is empty like this:

if (Array.isArray(a) && a.length) {
     // whatever you would like to do
} 

A very comprehensive explanation you can find in this response. So, looking at your concrete question that would mean the following:

const data = [[]]

if (Array.isArray(data[0]) && data[0].length === 0) {
     // whatever you would like to do
     console.log("empty array")
} 

Comments

0

You can just do:

yourNestedArray.flat().length > 0 ? 'some content' : 'im empty';

Flat a builtin function of the javascript Array class that flattens a multidimensional array by iterating through all nested items and concats them to a 1d array.

2 Comments

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
@Community Bot i have edit the comments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.