const regions = new Map();
regions.set("africa", new Map([
["1", "Cairo"],
["2", "Casablanka"],
["3", "Tunis"],
["4", "Maputo"],
]));
// Repeat for asia
You can then access the data like this:
// regions.keys() = ["africa", "asia"]
// regions.get("africa").keys() = ["1", "2", "3", "4"]
// regions.get("africa").get("1") = Cairo
This is a simple example using ES6 Maps in JavaScript. For a direct translation, consider using a tool like Json2CSharp that generates object structures with properties such as value
and text
.
Another approach using custom objects:
class RegionDB {
constructor() {
this.continents = new Map();
}
getContinents() {
return this.continents.keys();
}
getCitiesInContinent(continent) {
return this.continents.has(continent) ? this.continents.get(continent) : [];
}
setCitiesForContinent(continent, cities) {
this.continents.set(continent, cities || []);
}
}
class City {
constructor(text, value) {
this.text = text;
this.value = value;
}
}
Example usage:
const regiondb = new RegionDB();
regiondb.setCitiesForContinent("africa", [
new City("Cairo", "1"),
new City("Casablanka", "2"),
new City("Tunis", "3"),
new City("Maputo", "4")
]);
regiondb.setCitiesForContinent("asia", [
new City("Baku", "1"),
new City("Seul", "2"),
new City("Tokio", "3"),
new City("Ulan-Batar", "4"),
new City("Stambul", "5")
]);
// List data
for (const continent of regiondb.getContinents()) {
console.log(continent);
// List cities within
for (const city of regiondb.getCitiesInContinent(continent)) {
console.log(`value = ${city.value}, text = ${city.text}`);
}
}
Output:
africa
value = 1, text = Cairo
value = 2, text = Casablanka
value = 3, text = Tunis
value = 4, text = Maputo
asia
value = 1, text = Baku
value = 2, text = Seul
value = 3, text = Tokio
value = 4, text = Ulan-Batar
value = 5, text = Stambul