如何在Chrome中将设置转换为数组? [英] How to convert a Set to an Array in Chrome?
本文介绍了如何在Chrome中将设置转换为数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将设置转换为数组?给出了转换设置为阵列,其中没有一个当前在Chrome浏览器中可用。
How to convert Set to Array? gives three answers for converting a Set to an Array, none of which currently work in the Chrome browser.
假设我有一个简单的Set
Let's say I have a simple Set
var set_var = new Set(['a', 'b', 'c']);
我可以迭代我的变量并将元素添加到空数组中
I can iterate through my variable and add the elements to an empty array
var array_var = [];
set_var.forEach(function(element){array_var.push(element)});
但是,有没有其他方法可以做到这一点,并提供更广泛的浏览器支持?
But are there any other ways to do this that have wider browser support?
推荐答案
为什么不尝试使用set iterator?
Why not give a try with set iterator?
function setToArray(set) {
var it = set.values(),
ar = [],
ele = it.next();
while(!ele.done) {
ar.push(ele.value);
ele = it.next();
}
return ar;
}
setToArray(new Set(['a', 'b', false, 0, 'c'])); // ["a", "b", false, 0, "c"]
这篇关于如何在Chrome中将设置转换为数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文