0

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 ??

2 Answers 2

2

The following makes use of some array methods, namely filter, map, and forEach, to format the data as requested.

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"]
]

let lookup = {};

cities.filter(city => city[0] === "c").forEach(country => {
  let matches = cities
    .filter(city => city[0] === country[1])
    .map(city => city[1]);
  lookup[country[2]] = matches;
});

console.log(lookup);

Edit: Here is a solution that doesn't use array functions.

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"]
];

let lookup = {};

for (let i = 0; i < cities.length; i++) {
  // Test if this is a country
  if (cities[i][0] === "c") {
    // Make sure we have not processed this country before
    if (!lookup[cities[i][2]]) {
      lookup[cities[i][2]] = [];
      
      let countryCode = cities[i][1];
      // Loop through cities again and match country code
      for (let j = 0; j < cities.length; j++) {
        if (cities[j][0] === countryCode) {
          lookup[cities[i][2]].push(cities[j][1]);
        }
      }
    }
  }
}

console.log(lookup);

Sign up to request clarification or add additional context in comments.

4 Comments

can we just use loops for this?? :D so I can. understand
Sure, a solution that purely includes loops would just loop through the array and do something like "if this is a country, loop through the other array and add all countries"
You could use reduce in place of forEach to remove the need for lookup.
the country in that array length is not same with the array of cities
0

Here's an example with loops:

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.