-3

A list of cities and their countries is given, stored in the following structure:

let data = [
  {
    country: 'Poland',
    city:    'Cracow',
  },
  {
    country: 'France',
    city:    'Paris',
  },
  {
    country: 'Poland',
    city:    'Warsaw',
  },
  {
    country: 'Poland',
    city:    'Wroclaw',
  },
  {
    country: 'USA',
    city:    'New-York',
  },
  {
    country: 'France',
    city:    'Nice',
  },
  {
    country: 'USA',
    city:    'Boston',
  },
]

Write a code that transforms the data structure into this one using loops:

{
 'Poland': ['Cracow', 'Warsaw', 'Wroclaw',],
 'France': ['Paris', 'Nice'],
 'USA': ['Boston', 'New-York' ],
}

My attempt:

    obj={};
    arr=[];
    for ( elem of data){
      arr.push(elem.city)
      obj[elem.country]=arr
    }
    console.log(obj)

I have assigned arr to element. How can correct the code to assign only appropriate cities to one country? As a result I have:

{ Poland: 
   [ 'Cracow',
     'Paris',
     'Warsaw',
     'Wroclaw',
     'New-York',
     'Nice',
     'Boston' ],
  France: 
   [ 'Cracow',
     'Paris',
     'Warsaw',
     'Wroclaw',
     'New-York',
     'Nice',
     'Boston' ],
  USA: 
   [ 'Cracow',
     'Paris',
     'Warsaw',
     'Wroclaw',
     'New-York',
     'Nice',
     'Boston' ] }

How can correct the code to assign only appropriate cities to one country?

  • You're currently assigning one `elem` to one key. What you want though is for the keys to have *an array* of `elem`s. So somewhere you'll need to add an array and `push` the `elem` into it… – deceze Nov 15 '19 at 10:23
  • Thank you . I have made changes, but now in array I have all cities for one country, but I need only appropriate cities. – Liudmila Kristinevich Nov 15 '19 at 10:33

4 Answers4

0

You could do with Array#reduce .Append the country name to the Object and assign array then push the city name .

a[b.country] = a[b.country] || [];

IF they country already exist its copy the old or else create new array

let data = [ { country: 'Poland', city: 'Cracow', }, { country: 'France', city: 'Paris', }, { country: 'Poland', city: 'Warsaw', }, { country: 'Poland', city: 'Wroclaw', }, { country: 'USA', city: 'New-York', }, { country: 'France', city: 'Nice', }, { country: 'USA', city: 'Boston', }, ];

let res = data.reduce((a,b)=>{
    a[b.country] = a[b.country] || [];
    a[b.country].push(b.city);
    return a
},{});

console.log(res)
prasanth
  • 22,145
  • 4
  • 29
  • 53
0

You can do something like this

const grouped = {};

data.forEach(({country, city}) => {
    if (grouped[country]) grouped[country].push(city);
    else grouped[country] = [city];
});
Abito Prakash
  • 4,368
  • 2
  • 13
  • 26
0

You simply forgot the push and to check if the country object exists.

const data=[{country:"Poland",city:"Cracow"},{country:"France",city:"Paris"},{country:"Poland",city:"Warsaw"},{country:"Poland",city:"Wroclaw"},{country:"USA",city:"New-York"},{country:"France",city:"Nice"},{country:"USA",city:"Boston"}];

const res = {};

for(const {country, city} of data){
  if(!res[country]) res[country] = [];
  res[country].push(city);
}

console.log(res);
kemicofa ghost
  • 16,349
  • 8
  • 82
  • 131
0

Using modern Javascript you could do it like this:

const result = data.reduce((p, c) => {
  p[c.country] = [...(p[c.country]||[]), c.city]
  return p;
}, {});
hoeni
  • 3,031
  • 30
  • 45