本文翻译自:How to convert Set to Array?
Set seems like a nice way to create Arrays with guaranteed unique elements, but it does not expose any good way to get properties, except for generator [Set].values, which is called in an awkward way of mySet.values.next()
. Set似乎是一种创建具有保证唯一元素的Arrays的好方法,但它没有暴露任何获取属性的好方法,除了生成器[Set] .values,它以mySet.values.next()
的笨拙方式mySet.values.next()
。
This would have been ok, if you could call map
and similar functions on Sets. 如果您可以在集合上调用map
和类似函数,那就没问题了。 But you cannot do that, as well. 但你也不能这样做。
I've tried Array.from
, but seems to be converting only array-like (NodeList and TypedArrays ?) objects to Array. 我尝试过Array.from
,但似乎只是将类似数组(NodeList和TypedArrays?)的对象转换为Array。 Another try: Object.keys
does not work for Sets, and Set.prototype does not have similar static method. 另一个尝试: Object.keys
不适用于Sets,而Set.prototype没有类似的静态方法。
So, the question: Is there any convenient inbuilt method for creating an Array with values of a given Set ? 所以,问题是 : 是否有任何方便的内置方法来创建具有给定Set值的数组? (Order of element does not really matter). (元素的顺序并不重要)。
if no such option exists, then maybe there is a nice idiomatic one-liner for doing that ? 如果没有这样的选择,那么也许有一个很好的惯用的单行代码呢? like, using for...of
, or similar ? 比如, for...of
或类似的?
#1楼
参考:https://stackoom.com/question/1MD4u/如何将Set转换为Array
#2楼
if no such option exists, then maybe there is a nice idiomatic one-liner for doing that ? 如果没有这样的选择,那么也许有一个很好的惯用的单行代码呢? like, using for...of, or similar ? 比如,用于...或类似的?
Indeed, there are several ways to convert a Set to an Array : 实际上,有几种方法可以将Set转换为数组 :
using Array.from
使用Array.from
let array = Array.from(mySet);
Simply spreading
the Set out in an array 简单地spreading
Set spreading
为数组
let array = [...mySet];
The old fashion way, iterating and pushing to a new array (Sets do have forEach
) 旧的时尚方式,迭代并推送到一个新的数组(集合有forEach
)
let array = [];
mySet.forEach(v => array.push(v));
Previously, using the non-standard, and now deprecated array comprehension syntax: 以前,使用非标准,现在不推荐使用的数组解析语法:
let array = [v for (v of mySet)];
#3楼
via https://speakerdeck.com/anguscroll/es6-uncensored by Angus Croll 通过https://speakerdeck.com/anguscroll/es6-由Angus Croll提供
It turns out, we can use spread
operator: 事实证明,我们可以使用spread
运算符:
var myArr = [...mySet];
Or, alternatively, use Array.from
: 或者,使用Array.from
:
var myArr = Array.from(mySet);
#4楼
Assuming you are just using Set
temporarily to get unique values in an array and then converting back to an Array, try using this: 假设您只是暂时使用Set
来获取数组中的唯一值,然后转换回Array,请尝试使用:
_.uniq([])
This relies on using underscore or lo-dash . 这依赖于使用下划线或lo-dash 。
#5楼
Perhaps to late to the party, but you could just do the following: 也许到了晚会,但你可以做到以下几点:
const set = new Set(['a', 'b']);
const values = set.values();
const array = Array.from(values);
This should work without problems in browsers that have support for ES6 or if you have a shim that correctly polyfills the above functionality. 这应该在支持ES6的浏览器中没有问题,或者如果你有一个正确填充上述功能的垫片。
Edit : Today you can just use what @c69 suggests: 编辑 :今天你可以使用@ c69建议的内容:
const set = new Set(['a', 'b']);
const array = [...set]; // or Array.from(set)
#6楼
使用spread运算符可以获得所需的结果
var arrayFromSet = [...set];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)