if no such option exists, then maybe there is a nice idiomatic
one-liner for doing that ? like, usingfor...of, or similar ?
Indeed, there are several ways to convert a Set to an Array:
- Using
Array.from:
Note: safer for TypeScript.
const array = Array.from(mySet);
- Simply
spreadingthe Set out in an array:
Note: Spreading a Set has issues when compiled with TypeScript (See issue #8856). It’s safer to use Array.from above instead.
const array = [...mySet];
- The old-fashioned way, iterating and pushing to a new array (Sets do have
forEach):
const array = [];
mySet.forEach(v => array.push(v));
- Previously, using the non-standard, and now deprecated array comprehension syntax:
const array = [v for (v of mySet)];