Javascript 如何将映射键转换为数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35341696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert Map keys to array?
提问by Lilleman
Lets say I have the following map:
假设我有以下地图:
let myMap = new Map().set('a', 1).set('b', 2);
And I want to obtain ['a', 'b'] based on the above. My current solution seems so long and horrible.
我想根据上述获得 ['a', 'b'] 。我目前的解决方案似乎如此漫长和可怕。
let myMap = new Map().set('a', 1).set('b', 2);
let keys = [];
for (let key of myMap)
keys.push(key);
console.log(keys);
There must be a better way, no?
一定有更好的方法,不是吗?
回答by pawel
Map.keys()
returns a MapIterator
object which can be converted to Array
using Array.from
:
Map.keys()
返回一个MapIterator
可以转换为Array
using的对象Array.from
:
let keys = Array.from( myMap.keys() );
// ["a", "b"]
EDIT: you can also convert iterable object to array using spread syntax
编辑:您还可以使用扩展语法将可迭代对象转换为数组
let keys =[ ...myMap.keys() ];
// ["a", "b"]
回答by Fernando Carvajal
You can use the spread operator to convert Map.keys()iterator in an Array.
您可以使用扩展运算符将Map.keys()迭代器转换为数组。
let myMap = new Map().set('a', 1).set('b', 2).set(983, true)
let keys = [...myMap.keys()]
console.log(keys)
回答by linfluX
I need something similiar with angular reactive form:
我需要类似于角反应形式的东西:
let myMap = new Map().set(0, {status: 'VALID'}).set(1, {status: 'INVALID'});
let mapToArray = Array.from(myMap.values());
let isValid = mapToArray.every(x => x.status === 'VALID');
回答by Joman68
Array.from(myMap.keys())
does not work in google application scripts.
Array.from(myMap.keys())
在谷歌应用程序脚本中不起作用。
Trying to use it results in the error TypeError: Cannot find function from in object function Array() { [native code for Array.Array, arity=1] }
.
尝试使用它会导致错误TypeError: Cannot find function from in object function Array() { [native code for Array.Array, arity=1] }
。
To get a list of keys in GAS do this:
要获取 GAS 中的密钥列表,请执行以下操作:
var keysList = Object.keys(myMap);
回答by Tkoma
Not exactly best answer to question but this trick new Array(...someMap)
saved me couple of times when I need both key and value to generate needed array. For example when there is need to create react components from Map object based on both key and value values.
不是问题的最佳答案,但是new Array(...someMap)
当我需要键和值来生成所需的数组时,这个技巧为我节省了几次。例如,当需要基于键值和值值从 Map 对象创建反应组件时。
let map = new Map();
map.set("1", 1);
map.set("2", 2);
console.log(new Array(...map).map(pairs => pairs[0])); -> ["1", "2"]
回答by Alireza
OK, let's go a bit more comprehensive and start with what's Mapfor those who don't know this feature in JavaScript... MDN says:
好的,让我们更全面一点,从什么是Map开始,对于那些不了解 JavaScript 中这个特性的人...... MDN 说:
The Map object holds key-value pairs and remembers the original insertion order of the keys.
Any value (both objects and primitive values) may be used as either a key or a value.
Map 对象保存键值对并记住键的原始插入顺序。
任何值(对象和原始值)都可以用作键或值。
As you mentioned, you can easily create an instance of Map using new keyword... In your case:
正如您所提到的,您可以使用 new 关键字轻松创建 Map 实例...在您的情况下:
let myMap = new Map().set('a', 1).set('b', 2);
So let's see...
那么让我们看看...
The way you mentioned is an OK way to do it, but yes, there are more concise ways to do that...
您提到的方法是一种不错的方法,但是是的,有更简洁的方法可以做到这一点......
Maphas many methods which you can use, like set()
which you already used to assign the key values...
Map有许多您可以使用的方法,例如set()
您已经用于分配键值的方法......
One of them is keys()
which returns all the keys...
其中之一是keys()
返回所有键...
In your case, it will return:
在您的情况下,它将返回:
MapIterator {"a", "b"}
and you easily convert them to an Array using ES6ways, like spread operator...
并且您可以使用ES6方式轻松将它们转换为数组,例如展开运算符...
const b = [...myMap.keys()];