I am trying to extract all the cities from an object that contains country names as keys and arrays of cities as values. However, my approach seems to be failing and I can't figure out why.
var cities = {
"United Kingdom": ['london'],
"Spain": ['ibiza', 'malaga'],
"USA": ['hollywood']
}
var allCities = [];
for (c in cities) {
allCities.concat(cities[c]);
}
console.log(allCities); // This returns an empty array
When I try console.log(cities[c])
instead of allCities.concat(cities[c])
, I get each city array printed individually:
['london']
['ibiza', 'malaga']
['hollywood']
This issue is quite frustrating for me. Can anyone help identify why this code isn't working as expected?