How do you convert a `Set` to an array in JavaScript?
Topics
JavaScript
TL;DR
To convert a Set to an array in JavaScript, you can use the Array.from() method or the spread operator. For example:
Using Array.from()
The Array.from() method creates a new, shallow-copied array instance from an array-like or iterable object, such as a Set.
Using the spread operator
The spread operator (...) can be used to expand the elements of a Set into an array.
Performance considerations
Both Array.from(set) and [...set] iterate the Set into a new array. If conversion is a performance bottleneck, benchmark both methods with representative data.
Further reading
Exercises
Check your understanding
Beta