javascript Immutablejs:将地图的键转换为数组的一行代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30002578/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 11:28:22  来源:igfitidea点击:

Immutablejs: One liner code to convert keys of a map into array?

javascriptimmutable.js

提问by Melvin

From the docs: Map#keys

来自文档:Map#keys

I get the keys of a Map and loop through it to transform them into an array. Is there a one line code to cleanly convert these keys into an array?

我获取 Map 的键并循环遍历它以将它们转换为数组。是否有一行代码可以将这些键干净地转换为数组?

回答by OlliM

You can use keySeqinstead of keys, an IndexedSeqhas toArraymethod:

您可以使用keySeq代替keys, 一个IndexedSeqhastoArray方法:

var map = Immutable.fromJS({
  a: 1,
  b: 2,
  c: {
    d: "asdf"
  }
})

var arr = map.keySeq().toArray()

回答by geeklain

If you can use ES6:

如果您可以使用 ES6:

var map = Immutable.fromJS({
  a: 1,
  b: 2,
  c: {
    d: "asdf"
  }
});

var [...arr] = map.keys();
console.log(arr); // ["a", "b", "c"]

Or

或者

var arr = Array.from(map.keys());
console.log(arr); // ["a", "b", "c"]