检查javascript数组值是否不为空

检查javascript数组值是否不为空,javascript,json,Javascript,Json,在检查json响应中的数组值是否为空时,我有点困惑 { "status": "success", "message": "All pages re-ordered", "content": { "_wysihtml5_mode": "1", "Page": { "title": "cover", "page_text": "dfvdfvdfvdfvdv story go daddy xxx", "storyborad_i

在检查json响应中的数组值是否为空时,我有点困惑

{
  "status": "success",
  "message": "All pages re-ordered",
  "content": {
    "_wysihtml5_mode": "1",
    "Page": {
        "title": "cover",
        "page_text": "dfvdfvdfvdfvdv story go daddy xxx",
        "storyborad_img": "1jhkjh.png",
        "background_url": "kjbj.png",
        "newBackground_url": "",
        "text_font": "arial",
        "id": "30",
        "book_id": "38",
        "newStoryborad_img": {
            "name": "1jhkjh.png",
            "type": "image\/png",
            "tmp_name": "\/Applications\/MAMP\/tmp\/php\/phpvyf8Xx",
            "error": 0,
            "size": 185607
        }
    },
    "User": {
        "username": "testuser"
    }
  }
}
我试图检查
typeof array==undefined
.length
,但两者都给出了else语句
newBackground\u url

var page = $.parseJSON(xhr.responseText.replace('</p>', ''));
var imageType;
if(page.content.Page.newStoryborad_img.length > 0) {
    imageType = page.content.Page.newStoryborad_img.name;
}
else {
    imageType = page.content.Page.newBackground_url.name;
}
var page=$.parseJSON(xhr.responseText.replace(“

”,“”); var图像类型; 如果(page.content.page.newStoryborad_img.length>0){ imageType=page.content.page.newStoryborad\u img.name; } 否则{ imageType=page.content.page.newBackground\u url.name; }
视情况而定。根据您的示例,您正在返回一个对象
“newStoryborad\u img”:{…}

在这种情况下,您需要使用
typeof(page.content.page.newStoryborad\u img)!='未定义的“

演示类型:

但是,如果您正在查看对象数组
“newStoryborad_img”:[{…}]
(注意括号)。然后检查数组中是否存在任何对象(假设返回的是空数组)

page.content.page.newStoryborad\u img.length>0
将是您想要的


演示长度:

为什么JSON中有一个

?根据您的代码,路径应该是
page.content.page.storyborad\u img
[sic]。

是响应的一部分,但我正在改变它,以便在代码重构后将其删除。
newStoryborad\u img
不是一个对象吗?或者,当有多个图像时,您是否收到
数组
typeof(array)==undefined
应该给出else语句,因为您已经在json中定义了它。您可能需要
typeof(array)!='未定义的“
如果您希望它的存在触发
if
块。(这就是你想要它做的)谢谢你的帮助。如果我更改“newStoryborad\u img”:“我得到的是If而不是else作为typeof。我会一直在传递newStoryborad\u img,要么是空的,要么是带着values@KeithPower,这是对@JTG的答案/小提琴的更新,将if语句条件更改为
typeof(page.content.page.newStoryborad\u img)!='字符串“
表示它是一个对象或任何其他非字符串的对象,如果是,则返回console.log。如果是字符串,即“”,则为console.log-else。