Using a map function on a 'Map' to change values
Asked Answered
M

6

12

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?

Monteria answered 24/4, 2019 at 8:56 Comment(4)
Use Map#entries then .map on them a.entries().map(...)Mooney
@Mooney It won't work because Map.entries() return an Iterator (not Array); and Iterators haven't .map method...Cristencristi
Unfortunately, there is no elegant way. You have to process the entries and then produce a new Map out of the result.Diu
Possible duplicate of Using map/filter behavior on Map instanceHyetology
B
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)]
));
Bodhisattva answered 24/4, 2019 at 9:4 Comment(0)
B
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"}
Barny answered 24/4, 2019 at 10:14 Comment(0)
H
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));
Hyetology answered 24/4, 2019 at 14:10 Comment(0)
B
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);
Blodget answered 24/4, 2019 at 9:12 Comment(2)
for (let item of iterator1) that should be iterator, no?Diu
And the question was: 'in a functional way (without for ... of or .forEach)'. This example uses for...ofCristencristi
O
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)))
Ornithorhynchus answered 24/4, 2019 at 9:26 Comment(0)
F
0

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);
}
Fatigued answered 10/2 at 23:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.