Oh dear, I'm feeling quite perplexed right now. I created a JSON element in the following way:
var planet = {"continent": []};
planet.continent.push({name: "Asia", flag: "yes", countries: []});
planet.continent[0].countries.push({name: "Japan", capital: "Tokyo"});
This situation has raised two questions in my mind:
- Am I mixing arrays with JSON objects? How can I handle this without using arrays here?
- Is there a way to dynamically add elements to the JSON root?
As for question 2, I'm struggling because I don't know how to add elements directly to the root. I attempted the following approach, but it didn't deliver the desired result:
var index = 0;
var galaxy = {};
world.index.push({name: WA});
Therefore, I am hoping to figure out how to iterate over a previously created array and add elements accordingly.