Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set multiple values in a Javascript Map at once?

I have a simple chain of logic here, where bigCities is a Javascript Map. In this example, d represents each object in an array of data read in from a csv file. For every object's city property, I'm assigning it's d.pop value (the population of the city).

bigCities.set(d.city, +d.pop)

What if I want to be able to set multiple values at once? Would the code look something like this:

bigCities.set(d.city, ["population": +d.pop, "latitude": +d.lat, "longtitude": +d.lng)

Is it possible to create a key value pair in a Javascript Map, where the value is an array of data? If so, how would I write the above example correctly?

like image 508
Harrison Cramer Avatar asked Dec 03 '22 20:12

Harrison Cramer


2 Answers

To set multiple keys and values at a Map object you can pass an array of arrays

let m = new Map([["a", [1,2,3]], ["b", [4,5,6]]]);

console.log([...m], m.get("a"))
like image 193
guest271314 Avatar answered Dec 28 '22 07:12

guest271314


const map = new Map();

    map.set("a", "alpha").set("b", "beta").set("c", "charlie");

    console.info(map); // Map(3) {'a' => 'alpha', 'b' => 'beta', 'c' => 'charlie'}
like image 39
Doni Darmawan Avatar answered Dec 28 '22 06:12

Doni Darmawan