volume one
volume one

Reputation: 7563

Why does this array, which is not empty, return as empty?

Really struggling to understand why my conditions are resolving that the array is empty when it isn't!

Here is the code:

const info = [
    [
        {
            "Post": 7
        }
    ]
]

let Result;
     if (!Array.isArray(info[0][0]) || info[0][0].length === 0) {
        Result = {"Error": "No info"}
     }

Even though info has data in it, I still get back Result as {"Error: "No info"}.

Why are my if conditions not working properly? I think its something to do with the !Array.isArray(info[0][0]) part but not sure exactly what.

UPDATE:

If there is no Post then info becomes just this:

const info = [ [ ] ]

Thats why I need to check whether info[0][0] is empty or not

Upvotes: 0

Views: 44

Answers (2)

programmerRaj
programmerRaj

Reputation: 2058

I believe you have a bug when you call Array.isArray. You gave the input info[0][0], but you want to check if Array[0] is an array.

New code with fixed bug:

const info = [
    [
        {
            "Post": 7
        }
    ]
]

let Result;
     if (!Array.isArray(info[0]) || info[0].length === 0) {
        Result = {"Error": "No info"}
     }

Edit: Also removed extra [0] when checking length.

Upvotes: 1

Aleksey Polonka
Aleksey Polonka

Reputation: 583

Array.isArray returns false because info[0][0] is { "Post": 7 }

Upvotes: 0

Related Questions