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() })),);