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:
- Using
Array.from:
Note: safer for TypeScript.
const array = Array.from(mySet);
- Simply
spreadingthe Set out in an array:
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));
Answer from adeneo on Stack Overflowif 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:
- Using
Array.from:
Note: safer for TypeScript.
const array = Array.from(mySet);
- Simply
spreadingthe Set out in an array:
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));
via https://speakerdeck.com/anguscroll/es6-uncensored by Angus Croll
It turns out, we can use spread operator:
var myArr = [...mySet];
Or, alternatively, use Array.from:
var myArr = Array.from(mySet);
JavaScript Array to Set - Stack Overflow
I discovered Set exists for the first time today... Is it really faster?
How can I turn a Set into an Array?
how to get unique set of array of objects in javascript
Videos
Just pass the array to the Set constructor. The Set constructor accepts an iterable parameter. The Array object implements the iterable protocol, so its a valid parameter.
var arr = [55, 44, 65];
var set = new Set(arr);
console.log(set.size === arr.length);
console.log(set.has(65));
See here
If you start out with:
let array = [
{name: "malcom", dogType: "four-legged"},
{name: "peabody", dogType: "three-legged"},
{name: "pablo", dogType: "two-legged"}
];
And you want a set of, say, names, you would do:
let namesSet = new Set(array.map(item => item.name));
I was working through a problem in Angular, and finally had all the data I wanted to process pieced together.
I am very curious whether Set() is a common thing these days and whether I should consider learning more about these ES6 operator even if nobody on my team has ever used them (I have the least experience by a mile).
My first instinct was to go for an array, but then I asked ChatGPT to help me finish up the code, and it switched to a Set(). When I asked why, it said Set() is faster and excludes duplicates from the array.
The task consisted of:
Array 1: An array of arrays (a ton of data inside them),
Array 2: An array of 2+ objects with some string properties
Array 3: An array of objects with about 90 objects in it
The purpose is:
Loop through each object of Array 1 and find if the status string is what we want. If so we add the ID from that object to the targetId's Set().
Use the list of 90 objects and check if the ID we added in Step 1 exists in an object somewhere in there
If it exists in there, use the item.category to access the number value from categoryCounts object and increment it.
Here is a generic version of the code:
const categoryCounts: {[category: string]: number} = {};// Extract IDs from the dataconst targetIds = new Set < number > ();const segments = [...data.segmentOneArray,...data.segmentTwoArray,...data.segmentThreeArray,...data.segmentFourArray,];segments.forEach((segment) => {if (segment.status === "statusWeWant" && segment.itemId) {targetIds.add(segment.itemId);}});// Process the item list to count occurrences for each categoryitemList.forEach(item => {if (targetIds.has(Number(item.itemId))) {const category = item.category;if (filteredCategories.some(c => c.description === category)) {if (!categoryCounts[category]) {categoryCounts[category] = 0;}categoryCounts[category]++;}}})