How to Convert a JavaScript Array to Set?

If you pass an iterable (such as an array) to a Set constructor, then all of its elements will be added to the new Set. For example:

// ES6+
const arr = [1, 2, 3, 3, 4];
const set = new Set(arr);

console.log(set); // Set(4) { 1, 2, 3, 4 }

In this way, you can easily convert a JavaScript array to a Set object.


Hope you found this post useful. It was published . Please show your love and support by sharing this post.