在Java中我们可以创建
private map<String, List<String>> map = HashMap<String, ArrayList<String>();
如何使用JavaScript中的键和值列表创建地图?
我想将密钥作为国家/地区名称,值列表是国家/地区状态。如何在JavaScript中使用?
答案 0 :(得分:2)
map = {"country1": ["state1", "state2"], "country2": ["state1", "state2"]}
动态
var map = {};
map["country1"] = ["state1", "state2"];
map["country2"] = ["state1", "state2"];
答案 1 :(得分:0)
这里有Java和JavaScript编码器。
//Variable map, contains a instance of Map with a constructor of String and Array of String.
let map = new Map([[String.prototype, [String.prototype]]]);
//Put values in map.
map.set('Country1', ['State1', 'State2']);
map.set('Country2', ['State1', 'State2', 'State3']);
map.set('CountryN', ['State1', 'State2', 'State3', 'StateN']);
//Iterate map and print the content of this.
map.forEach((value, key) => {
console.log(`Country: ${key} States:[${value}]`);
});