0

I have a JSON array of objects out of which I need to create an Object of countries and continents like below:

For ex-

{
 "Asia":['India','China'],
 "Europe": ['Germany','United Kingdom']
}

So basically in above object continents would be a key and all the countries corresponding to that continent would come into array as a value.

This is what I want-

  1. Unite Kingdom should replace with UK inside an object.

Below is my code but its giving a wrong output:

   const arr = [
         {
            'id':1,
            'geography':'region',
            'zone':'Europe',
            'countries':['United Kingdom','France','Germany']
         },
         {
            'id':1,
            'geography':'global',
            'zone':'global',
         },
         {
            'id':1,
            'geography':'region',
            'zone':'Europe',
            'countries':['France','Germany','Netherlands']
         }
        ];
        
const obj = {};

for(let i=0; i<arr.length; i++) {
 //Check if geography is region only
 if(arr[i].geography === 'region') {
    //Checking if property is already exists in object
    if(obj[arr[i].zone]) {
        obj[arr[i].countries].map((items) => {
           obj[arr[i].zone].push(items);     
        });
    }
    //If property is not there then insert
    else{
        obj[arr[i].zone] = obj[arr[i].countries];
    }
  }
}  

console.log(obj);

Its giving me below output:

{ Europe: undefined }

Someone let me know what I am doing wrong in above code.

Digvijay
  • 2,887
  • 3
  • 36
  • 86
  • `obj[arr[i].zone] = obj[arr[i].countries];` - for starters, I think you want to assign `arr[i].countries` here, and not `obj[arr[i].countries]` – CBroe Apr 24 '23 at 09:03
  • And `obj[arr[i].countries].map()` should be `arr[i].countries.map()`. – CBroe Apr 24 '23 at 09:04
  • [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) and [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173) – VLAZ Apr 24 '23 at 09:04
  • Please do not use `.map()` for simple array iteration. Use `.forEach()` or a normal loop to do that. See: [Is performing a mapping operation without using returned value an antipattern?](https://stackoverflow.com/q/56903693) | [Is performing a mapping operation without using returned value an antipattern?](https://stackoverflow.com/q/56903693) | [Is there a difference between foreach and map?](https://stackoverflow.com/q/354909) | [What is the concept of Array.map?](https://stackoverflow.com/q/17367889) – VLAZ Apr 24 '23 at 09:04

1 Answers1

1

The issue is that you are trying to access the property of obj using the value of arr[i].countries, which is an array. You should instead use the key countries to get the array of countries, and then loop over it to add each country to the corresponding continent in the obj.

Also, to replace "United Kingdom" with "UK", you can use the replace method of the String object.

Try the following:

const arr = [
         {
            'id':1,
            'geography':'region',
            'zone':'Europe',
            'countries':['United Kingdom','France','Germany']
         },
         {
            'id':1,
            'geography':'global',
            'zone':'global',
         },
         {
            'id':1,
            'geography':'region',
            'zone':'Europe',
            'countries':['France','Germany','Netherlands']
         }
        ];

const obj = {};

for (let i = 0; i < arr.length; i++) {
  if (arr[i].geography === 'region') {
    if (obj[arr[i].zone]) {
      for (let j = 0; j < arr[i].countries.length; j++) {
        let temp = arr[i].countries[j].includes(' ')? arr[i].countries[j].match(/\b\w/g).join('').toUpperCase() : arr[i].countries[j];
        obj[arr[i].zone].push(temp);
      }
    } else {
      obj[arr[i].zone] = [];
      for (let j = 0; j < arr[i].countries.length; j++) {
        let temp = arr[i].countries[j].includes(' ')? arr[i].countries[j].match(/\b\w/g).join('').toUpperCase() : arr[i].countries[j];
        obj[arr[i].zone].push(temp);
      }
    }
  }
}

console.log(obj);
Mamun
  • 66,969
  • 9
  • 47
  • 59
  • What about let's say if I have `United States` I want to replace it with `US` instead so can I do that in the same replace statement. – Digvijay Apr 24 '23 at 09:09