11

I can use a Map and then set values:

const a = new Map([["a", "b"], ["c", "d"]])

Now if I want to apply a function to all values in a functional way (without for ... of or .forEach), I thought I could have done something like this:

const b = a.map(([k, v]) => [k, doSomethingWith(v)]);

But there is no map function on a Map. Is there a built-in/elegant way to map a Map?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
rap-2-h
  • 30,204
  • 37
  • 167
  • 263
  • Use `Map#entries` then `.map` on them `a.entries().map(...)` – ponury-kostek Apr 24 '19 at 09:02
  • @ponury-kostek It won't work because `Map.entries()` return an Iterator (not Array); and Iterators haven't `.map` method... – FZs Apr 24 '19 at 09:10
  • Unfortunately, there is no *elegant* way. You have to process the entries and then produce a new Map out of the result. – VLAZ Apr 24 '19 at 09:11
  • Possible duplicate of [Using map/filter behavior on Map instance](https://stackoverflow.com/q/49501690/1048572) – Bergi Apr 24 '19 at 13:57

5 Answers5

11

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)]
));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
4

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"}
nitzel
  • 1,565
  • 14
  • 14
2

There are no builtin methods for this (yet!). The most elegant way currently is to use generators:

const b = new Map((function*() {
    for (const [k, v] of a)
        yield [k, doSomethingWith(v)];
})());

I would however recommend to write helper functions for this that work with arbitrary iterables:

function* mapValue(iterable, callback) {
    for (const [k, v] of a)
        yield [k, callback(v)];
}
const b = new Map(mapValue(a, doSomethingWith));
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0

You could use Symbol.iterator for changing or creating a new Map.

function f (iterator) {
  for (let item of iterator1) {
    item[0] = 1;
    item[1] = 'hello world';
    console.log(item);
  }
}

var map1 = new Map();
map1.set('0', 'foo');
map1.set(1, 'bar');
var iterator1 = map1[Symbol.iterator]();

f(iterator1);
Κωλζαρ
  • 803
  • 1
  • 10
  • 22
  • 1
    `for (let item of iterator1)` that should be `iterator`, no? – VLAZ Apr 24 '19 at 09:13
  • And the question was: '*in a functional way (without for ... of or .forEach)*'. This example uses `for...of` – FZs Apr 24 '19 at 09:15
0

You could do that like this:

let b = new Map(a)
b.forEach((value,key,myMap) => myMap.set(key, dosomething(value)))

or:

let b
(b = new Map(a)).forEach((value,key,myMap) => myMap.set(key, dosomething(value)))
欧阳维杰
  • 1,608
  • 1
  • 14
  • 22