A short-hand way to do it is to convert it to an array via the ES6 spread operator.
Then all the array functions are available to you.
const mySet = new Set([1,2,3,4]);
[...mySet].reduce(...);
Answer from ZephDavies on Stack OverflowHow is a Set different from a Map in JavaScript?
A Set stores unique values, whereas a Map stores key-value pairs. Discover their differences in the Free JavaScript map set tutorial for beginners.
What is the purpose of a Map in JavaScript?
A Map object stores key-value pairs and allows keys of any data type, offering more flexibility than regular objects. Learn more in our JavaScript map set free course video.
Are Map and Set better than regular objects and arrays?
- Use Map when you need key-value pairs with non-string keys.
- Use Set for storing unique values without duplicates.
Find helpful insights in the JavaScript map set free course video.
A short-hand way to do it is to convert it to an array via the ES6 spread operator.
Then all the array functions are available to you.
const mySet = new Set([1,2,3,4]);
[...mySet].reduce(...);
To sum up the discussion from comments: while there are no technical reasons for set to not have reduce, it's not currently provided and we can only hope it changes in ES7.
As for map, calling it alone could violate the Set constraint, so its presence here might be debatable.
Consider mapping with a function (a) => 42 - it will change the set's size to 1, and this might or might not be what you wanted.
If you're ok with violating that because e.g. you're going to fold anyway, you can apply the map part on every element just before passing them to reduce, thus accepting that the intermediate collection (which isn't a Set at this point) that's going to be reduced might have duplicated elements. This is essentially equivalent to converting to Array to do processing.