Bogdan Galan Bogdan Galan - 3 months ago 7
JSON Question

loop through json arrays with jquery

My JSON looks like this:

{
"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"
}
]
}
]
}


My problem is that at Location 2 I get the same Mission results from Location 1.

Please see jsfiddle to know what I mean.
And maybe I'll get some help with setting the mission count correctly.

Answer

Your problem lies here: $(data.locations[0].missions).each... It's hardcored to 0. Use index instead.

Example: https://jsfiddle.net/aovb7wre/1/

Comments