I have been assigned the task of retrieving the cities from various countries, but I am unsure of the best approach to do so. How can I easily extract city names like:
For example, for USA it would be NYC and SFO.
I attempted using the code snippet cityData[0].children[0]
, however, it simply returns Object object
.
What is the most efficient way to access the cities for each country?
var cityData = [
{country: "USA", children:[
{"NYC": ["60%", "70%", "80%"]},
{"SFO": ["40%", "30%", "20%"]}
]},
{country: "Mexico", children:[
{"Mexico City": ["80%", "80%", "80%"]},
{"Cancun": ["20%", "20%", "20%"]}
]},
{country: "Canada", children:[
{"Toronto": ["50%", "60%", "60%"]},
{"Vancouver": ["50%", "40%", "40%"]}
]
}];
Is there an alternative method to retrieve the city name other than trying to access it with cityData[0].children['NYC']
and cityData[0].children['SFO']
?
I'm looking to target both cities with a single selector (if possible).
Feel free to suggest any changes to the data structure that might make this process easier.