Skip to main content

How to Convert an Array to a Set in JavaScript

A Set is a powerful, built-in JavaScript collection that stores only unique values. Converting an array to a Set is a common and highly efficient way to remove duplicate values from an array or to leverage the Set's high-performance has() method for checking the existence of elements.

This guide will teach you the modern, standard method for converting an array to a Set using the Set constructor. You will also learn how to handle an array of objects and how to convert a Set back into an array.

The most direct, concise, and readable way to create a Set from an array is to pass the array directly to the Set() constructor.

The Set() constructor accepts any iterable object (including arrays) as an argument. It automatically iterates over the elements and adds each one to the new Set, naturally discarding any duplicates in the process.

For example, we have an array with duplicate values and we want to create a Set containing only the unique values.

// Problem: How to get a Set with {'a', 'b', 'c'} from this array?
const myArray = ['a', 'b', 'a', 'c', 'b'];

Solution:

const myArray = ['a', 'b', 'a', 'c', 'b'];

// Pass the array directly to the Set constructor.
const mySet = new Set(myArray);

console.log(mySet); // Output: Set(3) { 'a', 'b', 'c' }

// You can now use Set methods
console.log(mySet.has('a')); // Output: true
console.log(mySet.size); // Output: 3

Output:

Set(3) {'a', 'b', 'c'}
true
3
note

This is the definitive best practice for this task.

The Inverse: Converting a Set Back to an Array

Once you have a Set, you might need to convert it back into an array to use array-specific methods like map() or sort(). The two modern ways to do this are the spread syntax (...) and Array.from().

Method 1: Spread Syntax (most concise)

const mySet = new Set(['a', 'b', 'c']);

const newArray1 = [...mySet];
console.log(newArray1);

Output:

['a', 'b', 'c']

Method 2: Array.from() (very readable)

const mySet = new Set(['a', 'b', 'c']);

const newArray2 = Array.from(mySet);
console.log(newArray2); // Output: ['a', 'b', 'c']

Output:

['a', 'b', 'c']
note

This Array -> Set -> Array round-trip is the most common and idiomatic way to get a new array with all duplicate values removed.

Working with an Array of Objects

If you have an array of objects and want to create a Set of the unique values of a specific property, you need to first extract those values into a new array using map().

For example, we want to get a Set of the unique letter properties from an array of objects.

// Problem: How to get a Set with {'a', 'b', 'c'} from this array of objects?
const myArray = [
{ id: 1, letter: 'a' },
{ id: 2, letter: 'b' },
{ id: 3, letter: 'a' }, // Duplicate letter
];

Solution: first, use map() to create an array of just the letter values, and then pass that array to the Set constructor.

const myArray = [
{ id: 1, letter: 'a' },
{ id: 2, letter: 'b' },
{ id: 3, letter: 'a' },
];

const letterValues = myArray.map(obj => obj.letter); // -> ['a', 'b', 'a']
const uniqueLetters = new Set(letterValues); // -> { 'a', 'b' }

console.log(uniqueLetters);

Output:

Set(2) { 'a', 'b' }
note

This can be written as a clean one-liner:

const uniqueLetters = new Set(myArray.map(obj => obj.letter));

A Note on Older, Verbose Methods

Before the Set object was common, this task was solved with more verbose methods, such as using forEach() or reduce() to manually build up a new array or object while checking for duplicates.

// ⛔️ This is an outdated and verbose way to create a Set.
const set = new Set();
myArray.forEach(element => {
set.add(element);
});
note

While these methods still work, they are not necessary for a simple conversion. The new Set(array) constructor is faster, more readable, and less error-prone.

Conclusion

Converting an array to a Set is a fundamental and simple operation in modern JavaScript.

  • The definitive best practice is to pass the array directly to the Set constructor: new Set(myArray).
  • This is the most efficient and readable way to remove duplicate values from an array.
  • To convert an array of objects to a Set of property values, use new Set(myArray.map(obj => obj.property)).