简体   繁体   English

为什么这个不为空的数组返回为空?

[英]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"} .即使info中有数据,我仍然返回Result as {"Error: "No info"}

Why are my if conditions not working properly?为什么我的if条件不能正常工作? I think its something to do with the .Array.isArray(info[0][0]) part but not sure exactly what.我认为它与.Array.isArray(info[0][0])部分有关,但不确定到底是什么。

UPDATE:更新:

If there is no Post then info becomes just this:如果没有Post ,那么info就变成了这样:

const info = [ [ ] ]

Thats why I need to check whether info[0][0] is empty or not这就是为什么我需要检查info[0][0]是否为空

I believe you have a bug when you call Array.isArray .我相信您在调用Array.isArray时遇到了错误。 You gave the input info[0][0] , but you want to check if Array[0] is an array.您提供了输入info[0][0] ,但您想检查Array[0]是否为数组。

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.编辑:检查长度时还删除了额外的[0]

Array.isArray returns false because info[0][0] is { "Post": 7 } Array.isArray 返回 false 因为 info[0][0] 是 { "Post": 7 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM