I got stuck to change the keys of my object result to be another keys of another object ,and maybe my code is not dynamic , I do love to get some hints from people here:
function country(arr) {
var obj = {},
temp = {}
for (i of arr) {
if (i.length === 2) {
if (!temp[i[0]])
temp[i[0]] = []
temp[i[0]].push(i[1])
}
if (i.length === 3) {
obj[i[2]] = []
}
}
console.log(obj) // final result
console.log(temp)
}
var cities = [
["c", "br", "Brazil"],
["br", "Rio de Jeneiro"],
["c", "usa", "United States"],
["ru", "St. Petersburg"],
["usa", "New York"],
["ksa", "Mekkah"],
["usa", "Washington DC"],
["usa", "California"],
["c", "ch", "China"],
["ksa", "Madinah"],
["ch", "Beijing"],
["c", "ind", "India"],
["ch", "Shanghai"],
["ind", "Bangalore"],
["ind", "New Delhi"],
["c", "ru", "Rusia"],
["ru", "Moscow"],
["c", "ksa", "Arab Saudi"]
]
console.log(country(cities))
the variable obj is the key I want to be output,
so the output I want is would be like this :
{
Brazil: [ 'Rio de Jeneiro' ],
'United States': [ 'New York', 'Washington DC', 'California' ],
............ rest...
}
is that possible to change the keys obj that temp object to be the keys in variable obj ??