使用jquery循环遍历json数组

时间:2016-09-01 15:06:59

标签: jquery json

我的JSON看起来像这样:

{
    "title": "Star Chart",
    "locations": [
        {
            "name": "Mercury",
            "faction": "Grineer",
            "enemyLvl": "6 - 11",
            "missCount": "11 Missions",
            "missions": [
                {
                    "name": "Apollodorus",
                    "target": "Infestation",
                    "type": "Survival",
                    "planet": "Mercury"
                },
                {
                    "name": "Boethius",
                    "target": "Infestation",
                    "type": "Exterminate",
                    "planet": "Mercury"
                }
            ]
        },
        {
            "name": "Venus",
            "faction": "Corpus",
            "enemyLvl": "3 - 18",
            "missCount": "14 Missions",
            "missions": [
                {
                    "name": "Aphrodite",
                    "target": "Corpus",
                    "type": "Mobile Defense",
                    "planet": "Venus"
                },
                {
                    "name": "Cytherean",
                    "target": "Corpus",
                    "type": "Interception",
                    "planet": "Venus"
                }
            ]
        }
    ]
}

我的问题是,在位置2,我从位置1获得相同的任务结果。

请参阅jsfiddle了解我的意思。 也许我会在正确设置任务计数方面得到一些帮助。

1 个答案:

答案 0 :(得分:0)

你的问题在于:$(data.locations[0].missions).each... 0已经很难了。请改用index

示例:https://jsfiddle.net/aovb7wre/1/