ES6 Map的Array.map相当于什么? [英] What's the equivalent of Array.map for ES6 Map?
本文介绍了ES6 Map的Array.map相当于什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我可以在数组上调用 map()
来遍历它,并返回另一个数组,如下所示:
So I can call map()
on an array to iterate through it and return another array, like so:
[1,2,3].map( (value, index) => value * index )
如果我想通过地图对象?
var myMap = new Map();
myMap.set(0, 1);
myMap.set(1, 2);
myMap.set(2, 3);
这无效:
myMap.map( (value, index) => value * index )
有一个forEach,但是不一样。
There's a forEach but that's not the same.
有一个快速的简写方法来从 Map
生成数组,例如 map ()
在数组上?
Is there a quick shorthand way to generate an array from a Map
, like map()
does on an array?
- 希望这句话有意义。
– hopefully that sentence makes sense.
推荐答案
必须是一个数组,那么您可以使用 Array.from()
和 Map.prototype.entries()
:
If the result must be an array then you can use the Array.from()
and Map.prototype.entries()
:
Array.from(myMap.entries()).map(([key, value]) => key * value)
或者你可以 spread 迭代器:
[...myMap.entries()].map(...)
这篇关于ES6 Map的Array.map相当于什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文