jQuery 2维数组 - 第二维未定义

时间:2013-10-20 22:02:17

标签: javascript jquery arrays json multidimensional-array

我使用$ .ajax来获取一个php文件的json请求。 JSON字符串不是我自己构建的。它是一个外部API。所以我不能以任何方式冒险。

jQuery遍历所有第一维条目。 对于'also_known_as',它还检查多个条目的第二维。

如果第二维中没有任何条目,则会出现问题。 如何检查是否至少有一个第二维 - 条目?

我试过了 if(typeof results[i].also_known_as[0] === "undefined")

if($.type(results[i].also_known_as[0]) === "undefined")

(也没有“”)

有什么想法吗?

JSON看起来像这样:

[
    {
        "plot": "Former cop ...",
        "genres": [
            "Action",
            "Crime",
            "Thriller"
        ],
        "rated": "PG-13",
        "rating": 7.3,
        "language": [
            "English",
            "Portuguese",
            "Spanish"
        ],
        "rating_count": 181888,
        "country": [
            "USA"
        ],
        "release_date": 20110429,
        "title": "Fast Five",
        "year": 2011,
        "filming_locations": "Rice, California, USA",
        "imdb_id": "tt1596343",
        "directors": [
            "Justin Lin"
        ],
        "writers": [
            "Chris Morgan",
            "Gary Scott Thompson"
        ],
        "actors": [
            "Vin Diesel",
            "Paul Walker",
            "Jordana Brewster",
            "Tyrese Gibson",
            "Ludacris",
            "Matt Schulze",
            "Sung Kang",
            "Gal Gadot",
            "Tego Calderon",
            "Don Omar",
            "Joaquim de Almeida",
            "Dwayne Johnson",
            "Elsa Pataky",
            "Michael Irby",
            "Fernando Chien"
        ],
        "plot_simple": "Dominic ...",
        "poster": {
            "imdb": "http://ia.media-imdb.com/images/M/MV5BMTUxNTk5MTE0OF5BMl5BanBnXkFtZTcwMjA2NzY3NA@@._V1_SY317_CR0,0,214,317_.jpg",
            "cover": "http://imdb-poster.b0.upaiyun.com/001/596/343.jpg!cover?_upt=6182835c1382348170"
        },
        "runtime": [
            "130 min"
        ],
        "type": "M",
        "imdb_url": "http://www.imdb.com/title/tt1596343/",
        "also_known_as": [
            {
                "country": "Argentina",
                "title": "Rápidos y furiosos 5in control"
            },
            {
                "country": "Australia",
                "title": "Fast & Furious 5"
            },
            {
                "remarks": [
                    "Bulgarian title"
                ],
                "country": "Bulgaria",
                "title": "Бързи и яростни 5: Удар в Рио"
            }
        ]
    },
    {
        "plot": "Former cop ...",
        "genres": [
            "Action",
            "Crime",
            "Thriller"
        ],
        "rated": "PG-13",
        "rating": 7.3,
        "language": [
            "English",
            "Portuguese",
            "Spanish"
        ],
        "rating_count": 181888,
        "country": [
            "USA"
        ],
        "release_date": 20110429,
        "title": "Fast Five",
        "year": 2011,
        "filming_locations": "Rice, California, USA",
        "imdb_id": "tt1596343",
        "directors": [
            "Justin Lin"
        ],
        "writers": [
            "Chris Morgan",
            "Gary Scott Thompson"
        ],
        "actors": [
            "Vin Diesel",
            "Paul Walker",
            "Jordana Brewster",
            "Tyrese Gibson",
            "Ludacris",
            "Matt Schulze",
            "Sung Kang",
            "Gal Gadot",
            "Tego Calderon",
            "Don Omar",
            "Joaquim de Almeida",
            "Dwayne Johnson",
            "Elsa Pataky",
            "Michael Irby",
            "Fernando Chien"
        ],
        "plot_simple": "Dominic ...",
        "poster": {
            "imdb": "http://ia.media-imdb.com/images/M/MV5BMTUxNTk5MTE0OF5BMl5BanBnXkFtZTcwMjA2NzY3NA@@._V1_SY317_CR0,0,214,317_.jpg",
            "cover": "http://imdb-poster.b0.upaiyun.com/001/596/343.jpg!cover?_upt=6182835c1382348170"
        },
        "runtime": [
            "130 min"
        ],
        "type": "M",
        "imdb_url": "http://www.imdb.com/title/tt1596343/"
    }
]

这是我的jQuery函数的片段:

$.each(results, function(i, item) {

    var id = results[i].imdb_id;
    var title = results[i].title;

    var also_known_as = '';
    var also_known_as_prio = ''; //1 = Switzerland, 2 = Germany, 3 = Austria ... Geringere Prio -> wichtiger -> Überschreibt weniger wichtige

    $.each(results[i].also_known_as, function(ii, iitem) {

        switch (results[i].also_known_as[ii].country) {
            case 'Switzerland':
                also_known_as = results[i].also_known_as[ii].title;
                also_known_as_prio = 1;
                break;
            case 'Germany':
                if (also_known_as_prio != 1) {
                    also_known_as = results[i].also_known_as[ii].title;
                    also_known_as_prio = 2;
                }
                break;
            case 'Austria':
                if (also_known_as_prio != 1 && also_known_as_prio != 2) {
                    also_known_as = results[i].also_known_as[ii].title;
                    also_known_as_prio = 3;
                }
                break;
        }

    });

});

1 个答案:

答案 0 :(得分:0)

我认为你想要typeof results[i].also_known_as === "undefined"(没有[0])。目前,代码正在尝试查找数组中的元素,然后检查该元素是否未定义;由于数组本身是未定义的,因此在尝试查找数组元素之前,它甚至会在检查元素是否未定义之前发生错误。

相关问题