I have an object containing countries, cities and shops in those cities. I want to show (later) countries sorted based on amount of cities in the country, which works (my code) and then I want to sort the cities based on the number of shops (so the city with most shops comes first). Ive tried for so long and just cant make it work because whatever I tried or do does not work. I need to access the 'shopNum' property and then access cities and sort but nothing I did/do works. Any help would be really great. Sorry if it seems maybe too basic but I tried for a while, researched etc and am kind of lost.
var data = {
Italy: {
cityNum: 2,
Rome: {
shops: ['Shop123', 'shopxyz'],
shopNum: 2
},
Milan: {
shops: ['Shop1', 'shopA', 'shopD'],
shopNum: 3
}
},
USA: {
cityNum: 1,
Chicago: {
shops: ['Shop123', 'shopxyz', 'shopZ'],
shopNum: 3
},
},
}
var sortedCountries = Object.keys(data).sort((a, b) => {
return data[b]["cityNum"] - data[a]["cityNum"];
}) //works fine
var sortedShops = Object.values(data).map(x => {
Object.values(Object.keys(x).map(item => {
if (item !== "cityNum") {
console.log(item)
return item;
}
})).map(x => console.log("item x", x))
})
sortedCities