rap-2-h
rap-2-h

Reputation: 32038

Using a map function on a 'Map' to change values

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?

Upvotes: 12

Views: 2623

Answers (6)

hubatish
hubatish

Reputation: 5250

For comparison (& because these methods were a little ugly..), here's how to modify each value of a map using a traditional for .. of loop:

for (let entry of map.entries()) {
  let key = entry[0], value = entry[1];
  let newValue = value + ' '; // Modify however.
  map.set(key, newValue);
}

Upvotes: 0

Bergi
Bergi

Reputation: 665080

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));

Upvotes: 2

nitzel
nitzel

Reputation: 1875

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"}

Upvotes: 4

欧阳维杰
欧阳维杰

Reputation: 1768

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)))

Upvotes: 0

Kolzar
Kolzar

Reputation: 873

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);

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386728

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)]
));

Upvotes: 11

Related Questions