0

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

5
  • the first answer is right also there is no sense in info[0][0].length Commented May 11, 2020 at 20:22
  • 1
    The first condition is True (not an array but an Object), the second is False (But it's not taken into account here), so Result is still undefined. You can check it here: stackblitz.com/edit/react-pn3vax?file=index.js It's working well I think. Did you miss some part of your code hat could change that perhaps ? Commented May 11, 2020 at 20:27
  • @QuentinGrisel thanks. please check my update for what happens when theres no Post Commented May 11, 2020 at 20:56
  • @volumeone Ok but I still don't understand where is the problem. !Array.isArray(info[0][0]) === true implicitly mean there is something inside your info. You stick to this I think Commented May 11, 2020 at 21:56
  • @QuentinGrisel it is undefined in your code because you used result instead of Result in the console.log() Commented May 11, 2020 at 22:49

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

thank you. can you please see my update... how can I test for when the second array contains something and for when it doesnt?
Are you sure it doesn't already do that? If it checks the length of the array to be zero it should work, right? Another way of checking it could be typeof info[0][0] == "object"
if an array contains an array then its not empty right? Wouldnt Array.isArray[0] be true for [ [ ] ] ?
Well, you're right that checking Array.isArray(info) would be true if info was [ [ ] ]. I was talking about when doing 'info[0].length === 0'. Since info[0] would be [], info[0] would have a length of zero. Does it still not work after testing it?
Yep it works. To get Post I have to do info[0][0].Post.... so to check if it was empty or not I was doing info[0][0].length which of course is wrong because that is checking the object's length inside the nested array when one didnt exist. Mind just went to jelly
0

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

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.