How to convert Set to Array?

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:

Note: safer for TypeScript.

const array = Array.from(mySet);

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)];

Leave a Comment