本文翻譯自: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];