Quantcast
Channel: How to convert Set to Array? - Stack Overflow
Viewing all articles
Browse latest Browse all 12

Answer by Darren Evans for How to convert Set to Array?

$
0
0

As a web dev newcomer, I've been experimenting with Sets.

mySet = new Set([1, 2, 3, 4, 5]);

Create an array from values in a Set:

const myArray = [...mySet];

Transform values in a Set using map method to an array:

const myArray = [...mySet].map((x) => x * 2);

After transforming with map method, maybe you want to store them back in a Set to use the newer Set instance methods like intersection() or difference() etc:

const transformedSet = new Set([...mySet].map((x) => x * 2));

Here's an example of transforming the values of a Set of objects instead of a simple array of numbers:

mySet = new Set([    { name: "John", age: 22 },    { name: "Jane", age: 25 },]);const transformedSet = new Set(    [...mySet].map((x) => ({ ...x, name: x.name.toUpperCase() })),);

Viewing all articles
Browse latest Browse all 12

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>