Solution
My approach to solving the problem involved creating three separate arrays - one for countries, one for states, and one for cities. Each array contains relevant information along with references to other arrays for easy access:
Country Array
{
country_id: index of country,
country: name of country
}
State Array
{
state_id: index of state,
state: name of state,
country_id: reference to country array
}
City Array
{
city_id: index of city,
city: name of city,
state_id: reference to state array
}
By using the Array.prototype.filter
function, I am able to retrieve states based on country ID and cities based on state ID.
Example Code
/**
* Example
* @type {Object}
*/
var country = {
"Countries": [{
"Country": "Country1",
"states": [{
"state": "state11",
"city": ["city111", "city112"]
}, {
"state": "state12",
"city": ["city121", "city122"]
}]
}, {
"Country": "Country2",
"states": [{
"state": "state23",
"city": ["city231", "city232"]
}, {
"state": "state24",
"city": ["city241", "city242"]
}]
}]
};
/**
* Default starting ID for state list
* @type {Number}
*/
var state_id = 0;
/**
* Default starting ID for city list
* @type {Number}
*/
var city_id = 0;
/**
* Array of country names
* @type {Array}
*/
var country_array = [];
/**
* Array of states (along with ID of country they belong to)
* @type {Array}
*/
var state_array = [];
/**
* Array of cities (along with ID of state they belong to)
* @type {Array}
*/
var city_array = [];
/////////////////
// THE PROCESS //
/////////////////
country.Countries
.forEach(function(each_country, country_index) {
country_array
.push({
country_id: country_index,
country: each_country.Country
});
each_country.states
.forEach(function(each_state) {
state_array
.push({
state_id: state_id,
state: each_state.state,
country_id: country_index
});
each_state.city
.forEach(function(each_city) {
city_array
.push({
city_id: city_id,
city: each_city,
state_id: state_id
});
city_id = city_array.length; // Calculating the next city_id
});
state_id = state_array.length; // Calculating the next state_id
});
});
/**
* Returns array of countries
* @return {[Object]} Array of countries
*/
var getCountryList = function() {
return country_array;
};
/**
* Returns array of states belonging to a country
* @param {Number} country_id The index of the country in the country array
* @return {[Object]} Array of states
*/
var getStateList = function(country_id) {
return state_array
.filter(function(each) {
return each.country_id === country_id;
});
};
/**
* Returns array of cities belonging to a state
* @param {Number} state_id The index of the state in the state array
* @return {[Object]} Array of cities
*/
var getCityList = function(state_id) {
return city_array
.filter(function(each) {
return each.state_id === state_id;
});
};
// Retrieve the country list
getCountryList();
// Retrieve the state list of country with ID 0
getStateList(0);
// Retrieve the state list of country with ID 1
getStateList(1);
// Retrieve the city list of state with ID 0
getCityList(0);
// Retrieve the city list of state with ID 1
getCityList(1);