Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a Set to an Array in 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.

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?

like image 985
Cecilia Avatar asked Apr 14 '15 22:04

Cecilia


1 Answers

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"]
like image 109
Leo Avatar answered Sep 26 '22 06:09

Leo