const 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"]];
const out = {};
const lookup = {};
// First loop creates a lookup of the
// short and long country names,
// and initialises the output object
for (let city of cities) {
if (city.length === 3) {
const [ , short, full ] = city;
out[full] = out[full] || [];
lookup[short] = full;
}
}
// Second loop finds the actual cities
// and, checks the lookup table, then adds them to
// the output object
for (let city of cities) {
if (city.length === 2) {
const [ short, full ] = city;
const check = lookup[short];
if (lookup[short]) out[check].push(full);
}
}
console.log(out);