[英]How to create map with key and a list of values in JavaScript
In Java we can create在 Java 中,我们可以创建
private map<String, List<String>> map = HashMap<String, ArrayList<String>();
How to create a map with keys and lists of values in JavaScript?如何在 JavaScript 中使用键和值列表创建地图?
I want to put key as country name and list of values are country states.我想把键作为国家名称,值列表是国家。 How to do in JavaScript?
在 JavaScript 中怎么做?
map = {"country1": ["state1", "state2"], "country2": ["state1", "state2"]}
OR dynamically或动态
var map = {};
map["country1"] = ["state1", "state2"];
map["country2"] = ["state1", "state2"];
Here a java and javascript coder.这里有一个 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}]`); });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.