how would you define an array in JavaScript like the one below and retrieve values by doing a search for country and gets the list of region.
region['US'] = {'Region 1': 'East', 'Region 2': 'West'}
how would you define an array in JavaScript like the one below and retrieve values by doing a search for country and gets the list of region.
region['US'] = {'Region 1': 'East', 'Region 2': 'West'}
You want an object, not an array. Objects have string keys, arrays have numeric keys only.
var region = {
US: {'Region 1': 'East', 'Region 2': 'West'},
UK: {'Region 1': 'East', 'Region 2': 'West'}
}
then get it with
region['US']
or
region.US
If we declare specific classes for the Country and the Region, this should work:
var Region = function(data) {
var self = this;
self.name = data.name || "";
}
var Country = function(data) {
var self = this;
self.name = data.name || "";
self.regions = new Array();
}
var countries = {};
countries["US"] = new Country({ "name" : "US" });
countries["US"].regions.push(new Region({ "name" : "Region 1"}));
countries["US"].regions.push(new Region({ "name" : "Region 2"}));