3

In Java we can create

private map<String, List<String>> map = HashMap<String, ArrayList<String>();

How to create a map with keys and lists of values in JavaScript?

I want to put key as country name and list of values are country states. How to do in JavaScript?

2 Answers 2

2
map = {"country1": ["state1", "state2"], "country2": ["state1", "state2"]}

OR dynamically

var map = {};
map["country1"] = ["state1", "state2"];
map["country2"] = ["state1", "state2"];
Sign up to request clarification or add additional context in comments.

2 Comments

how to create dynamic map?
Sharing a post that explains in a better way: stackoverflow.com/questions/33020554/…
1

Here a java and javascript coder.

//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}]`);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.