Learning to do tree structures of countries breakdown

Learning to do tree structures of countries breakdown
0.0 0

#1

Argentina is subdivided into 23 provinces and one city - am I doing this code right or wrong. Be easy I am only new learning basic parent child tree structures, if you know of any good links that would help me understand it would be great. Anyway here goes.

{  
   "divisions":{  
      "subArea":[  
         {  
            "provinces":{  
               "subArea":[  
                  {  
                     "name":"Buenos Aires",
                     "subCode":"AR-C"
                  },
                  {  
                     "name":"Catamarca",
                     "subCode":"AR-B"
                  },
                  {  
                     "name":"Chaco",
                     "subCode":"AR-H"
                  },
                  {  
                     "name":"Chubut",
                     "subCode":"AR-U"
                  },
                  {  
                     "name":"Córdoba",
                     "subCode":"AR-X"
                  },
                  {  
                     "name":" Corrientes",
                     "subCode":"AR-W"
                  },
                  {  
                     "name":" Entre Ríos",
                     "subCode":"AR-E"
                  },
                  {  
                     "name":"Formosa",
                     "subCode":"AR-P"
                  },
                  {  
                     "name":"Jujuy",
                     "subCode":"AR-Y"
                  },
                  {  
                     "name":"La Pampa",
                     "subCode":"AR-L"
                  },
                  {  
                     "name":"La Rioja",
                     "subCode":"AR-F"
                  },
                  {  
                     "name":"Mendoza",
                     "subCode":"AR-M"
                  },
                  {  
                     "name":"Misiones",
                     "subCode":"AR-N"
                  },
                  {  
                     "name":"Neuquén",
                     "subCode":"AR-Q"
                  },
                  {  
                     "name":"Río Negro",
                     "subCode":"AR-R"
                  },
                  {  
                     "name":"Salta",
                     "subCode":"AR-A"
                  },
                  {  
                     "name":"San Juan",
                     "subCode":"AR-J"
                  },
                  {  
                     "name":" San Luis",
                     "subCode":"AR-D"
                  },
                  {  
                     "name":"Santa Cruz",
                     "subCode":"AR-Z"
                  },
                  {  
                     "name":"Santa Fe",
                     "subCode":"AR-S"
                  },
                  {  
                     "name":"Santiago del Estero",
                     "subCode":"AR-G"
                  },
                  {  
                     "name":"Tierra del Fuego",
                     "subCode":"AR-V"
                  },
                  {  
                     "name":"Tucumán",
                     "subCode":"AR-T"
                  }
               ]
            },
            "city":{  
               "subArea":[  
                  {  
                     "name":"Ciudad Autónoma de Buenos Aires",
                     "subCode":"AR-C"
                  }
               ]
            }
         }
      ]
   }
}

#2

Is the above object already given to you are are you trying to figure out the best way to create/structute an object which contains all of the different provinces and relevant subCodes?

If you are creating your own structure, do you need to be able to lookup a province name based on a given subCode or do you need to be able to lookup up a subCode based on a give province? I ask because it would determine how I suggest for you to structure your object.

What are you trying to do with the information in the above object? This would help us give you better advice.

How does the “city” property of the “provinces” object factor into your needs? I noticed it contains an object with one property “subArea” which contains an array with one object. That object has a subCode with the same subCode as one of the provinces.


#3

It is code that I made up myself by using information from https://en.wikipedia.org/wiki/ISO_3166-2:AR. I would like to be able go to look Argentina and then be able to see it is made up of 1 city and 23 provinces and then eventually be able that someone would be able to do a search based on it ISO3166-2 code. Sorry if this is a mess, I am trying to figure it out myself as I go.


#4

I think this is the structure you want then.

var argentina = {
  provinces: {
    'AR-C': 'Buenos Aires',
    'AR-B': 'Catamarca',
    'AR-H': 'Chaco',
    'AR-U': 'Chubut',
    'AR-X': 'Córdoba',
    'AR-W': 'Corrientes',
    'AR-E': 'Entre Ríos',
    'AR-P': 'Formosa',
    'AR-Y': 'Jujuy',
    'AR-L': 'La Pampa',
    'AR-F': 'La Rioja',
    'AR-M': 'Mendoza',
    'AR-N': 'Misiones',
    'AR-Q': 'Neuquén',
    'AR-R': 'Río Negro',
    'AR-A': 'Salta',
    'AR-J': 'San Juan',
    'AR-D': 'San Luis',
    'AR-Z': 'Santa Cruz',
    'AR-S': 'Santa Fe',
    'AR-G': 'Santiago del Estero',
    'AR-V': 'Tierra del Fuego',
    'AR-T': 'Tucumán',
  },
  cities: {
    'Ciudad Autónoma de Buenos Aires': 'AR-C'
  },
};

Then if you have a subCode like ‘AR-M’, you can look up the province. Also, you can find the cities and using the subCode of a particular city, you can always reference back to which province it is located in. See below for an example.

// display the province for subCode 'AR-M'
console.log(argentina.provinces['AR-M']); // displays 'Mendoza'

// display all cities in argentina and which province they belong
for (var city in argentina.cities) {
  var subCode = argentina.cities[city];
  console.log('City: ' + city + ', Province: ' + argentina.provinces[subCode]);
}

#5

Thanks a million for your help, I tend to over complicate things when I dont need to.