admin管理员组文章数量:1206201
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
?
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
?
6 Answers
Reset to default 11You 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)]
));
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"}
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));
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);
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)))
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);
}
本文标签: javascriptUsing a map function on a 39Map39 to change valuesStack Overflow
版权声明:本文标题:javascript - Using a map function on a 'Map' to change values - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738672096a2106055.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
Map#entries
then.map
on thema.entries().map(...)
– ponury-kostek Commented Apr 24, 2019 at 9:02Map.entries()
return an Iterator (not Array); and Iterators haven't.map
method... – FZs Commented Apr 24, 2019 at 9:10