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.
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?
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"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With