Set
collections can be converted
to arrays. You can also perform the
opposite operation - convert arrays
into Set
. Let's see how it's
done.
Set-to-array conversion
Let we have some Set
collection:
let set = new Set([1, 2, 3]);
You can convert it to an array using the destructuring technique:
let arr = [...set];
Or you can use the Array.from
method:
let arr = Array.from(set);
Given a Set
collection. Convert
it to an array in the two described ways.
Array-to-Set conversion
Let's say we have an array:
let arr = [1, 2, 3];
Let's convert it to a Set
collection:
let set = new Set(arr);
Given an array. Convert it to a collection.