Using a map function on a 'Map' to change values
The most elegant/concise way I am aware of is by converting the map to an array with the spread operator (...
), applying .map
to that array and then creating a Map from it again:
const a = new Map([["a", "b"], ["c", "d"]])
const b = new Map([...a].map(([k,v])=>[k, v.toUpperCase()]))
// b: Map(2) {"a" => "B", "c" => "D"}
You could use Array.from
for getting entries and mapping new values and take this result for a new Map
.
const b = new Map(Array.from(
a,
([k, v]) => [k, doSomethingWith(v)]
));