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 spreading the 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 Overflow
Discussions

JavaScript Array to Set - Stack Overflow
MDN references JavaScript's Set collection abstraction. I've got an array of objects that I'd like to convert to a set so that I am able to remove (.delete()) various elements by name: var array = ... More on stackoverflow.com
🌐 stackoverflow.com
I discovered Set exists for the first time today... Is it really faster?
I use sets, maps, and spread syntax all the time. I’ve never “done the math” to see the specs of the speed for sets. Maps have methods that are way faster when trying to search and filter things in big data sets. More on reddit.com
🌐 r/learnjavascript
26
11
July 24, 2024
How can I turn a Set into an Array?
A Set is an effective way to maintain Arrays with unique values, but accessing properties from a Set is limited, usually involving the somewhat cumbersome mySet.values().next(). It would be fine if methods like map could be directly applied to Sets, but they cannot be used directly. More on community.latenode.com
🌐 community.latenode.com
0
0
October 11, 2024
how to get unique set of array of objects in javascript
Not sure how efficient mapping within the filter is, but: const arr = [{ id: 1 }, { id: 2 }, { id: 1 }]; const unique = arr.filter((v, i, a) => a.map(({ id }) => id).indexOf(v.id) === i); console.log(unique); That's assuming id is your primary key More on reddit.com
🌐 r/node
15
2
June 3, 2020
🌐
W3Schools
w3schools.com › js › js_arrays.asp
W3Schools.com
It is a common practice to declare arrays with the const keyword. Learn more about const with arrays in the chapter: JS Array Const.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Set
Set - JavaScript | MDN
js · const myArray = ["value1", "value2", "value3"]; // Use the regular Set constructor to transform an Array into a Set const mySet = new Set(myArray); mySet.has("value1"); // returns true // Use the spread syntax to transform a set into an Array. console.log([...mySet]); // Will show you ...
Find elsewhere
🌐
Medium
medium.com › @conboys111 › how-to-easily-convert-a-set-to-an-array-and-vice-versa-in-javascript-a4e28ac9ccef
How to Easily Convert a Set to an Array and Vice Versa in JavaScript | by myHotTake | Medium
November 4, 2024 - But what if I need to pack up my exhibit and return the artifacts to storage, while preserving the order and keeping everything (even duplicates) in my storage room? This is where the magic happens. I can easily convert my Set back into an Array, almost like unfolding the artifacts from the display case and putting them back into storage, but now the order might matter.
🌐
Scaler
scaler.com › home › topics › how to convert set to array in javascript
How to Convert Set to Array in JavaScript - Scaler Topics
May 4, 2023 - Explanation: We only gave an iterable object as a parameter to the Array.from() method. The method creates a new instance of the array and converts the iterable into an array. ... The Spread operator can also be used to turn the set into an array.
🌐
Favtutor
favtutor.com › articles › set-to-array-javascript
Convert Set to Array in JavaScript (3 Easy Methods)
November 14, 2023 - To create a set from an array, simply make a set with desired elements. Use the spread operator (…) followed by the Set name inside square brackets to spread the elements into a new Array. Finally, store the array in a new variable.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › TypedArray › set
TypedArray.prototype.set() - JavaScript | MDN
July 10, 2025 - js · set(array) set(array, targetOffset) set(typedarray) set(typedarray, targetOffset) array · The array from which to copy values.
🌐
Reddit
reddit.com › r/learnjavascript › i discovered set exists for the first time today... is it really faster?
r/learnjavascript on Reddit: I discovered Set exists for the first time today... Is it really faster?
July 24, 2024 -

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:

  1. 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().

  2. Use the list of 90 objects and check if the ID we added in Step 1 exists in an object somewhere in there

  3. 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 data
const 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 category
itemList.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]++;
}
}
})

🌐
Koladechris
koladechris.com › blog › what-are-the-differences-between-array-and-set-in-javascript
What are the Differences between Array and Set in JavaScript?
December 1, 2024 - An Array lets you store duplicates but a set does not. This makes arrays more suitable when you need values to be repeated.
🌐
Medium
medium.com › front-end-weekly › es6-set-vs-array-what-and-when-efc055655e1a
ES6 — Set vs Array — What and when? | by Maya Shavin | Frontend Weekly | Medium
November 9, 2018 - Yup, by definition, Array and Set are technically different concepts. One of the biggest differences here, you may notice, is that elements in Array can be duplicate (unless you tell it not to be), and in Set, they just can’t (regardless what you decide).
🌐
TutorialsPoint
tutorialspoint.com › how-to-convert-array-to-set-in-javascript
How to convert Array to Set in JavaScript?
August 16, 2023 - This tutorial will teach you how to eliminate duplicate values from an array of items in Javascript by converting an array to a set. When storing distinct values from a collection of data in javascript, we can utilise the Set data structure to get
🌐
WsCube Tech
wscubetech.com › resources › javascript › programs › array-to-set
Convert Array to Set in JavaScript (6 Programs With Output)
January 20, 2026 - Learn how to convert an array to a Set in JavaScript with 6 practical examples. Understand each method with simple code and output to master Set conversion easily.
🌐
LinkedIn
linkedin.com › pulse › array-vs-set-javascript-manvendra-singh-scjbf
Array vs Set in JavaScript
November 25, 2023 - No Index:Unlike arrays, sets do not have methods that rely on indexes since elements have no fixed order. Iteration:You can iterate through elements in a Set using forEach or by converting it to an array ([...mySet]).
🌐
Latenode
community.latenode.com › other questions › javascript
How can I turn a Set into an Array?
October 11, 2024 - A Set is an effective way to maintain Arrays with unique values, but accessing properties from a Set is limited, usually involving the somewhat cumbersome mySet.values().next(). It would be fine if methods like map coul…
🌐
DEV Community
dev.to › clairecodes › how-to-create-an-array-of-unique-values-in-javascript-using-sets-5dg6
How to create an array of unique values in JavaScript using Sets - DEV Community
May 8, 2018 - How to remove all duplicated values from an array in JavaScript by using the Set data object introduced in ES6. Tagged with javascript, webdev.
🌐
React
react.dev › learn › updating-arrays-in-state
Updating Arrays in State – React
There are multiple ways to do this, but the easiest one is to use the ... array spread syntax: setArtists( // Replace the state · [ // with a new array · ...artists, // that contains all the old items · { id: nextId++, name: name } // and one new item at the end · ] ); Now it works correctly: App.jsApp.js ·