How to Convert JavaScript Array to Set

A Set is a collection of values, where each value may occur only once. The Set objects are collections of values. You can iterate through the items of a set in insertion order. Because each value in the Set has to be unique, the value equality will be checked.

JavaScript Array to Set

To convert an array to a set in JavaScript, use the set() constructor. The Set constructor accepts an iterable parameter and returns the set. The Array object implements the iterable protocol, so it’s a valid parameter.

let moneyheist = ["Tokyo", "Nairobi", "Berlin", "Oslo", "Moscow"]

let moneyheist_set = new Set(moneyheist)

console.log(moneyheist_set)

Output

Set(5) { 'Tokyo', 'Nairobi', 'Berlin', 'Oslo', 'Moscow' }

The new keyword is to create a new set and pass the JavaScript array as its first and only argument. Next, you can see that the array is converted to the set.

One thing to keep in mind is that if your array has repeated values, then only one value among the repeated values will be added to your Set. Duplicate values will be removed from the set because the set can’t contain duplicates. This can be helpful when you want to get unique values from the array.

Using Array.prototype.map() function

Use the array.map() function to transform a regular array into a set. The map() is a built-in JavaScript method that creates a new array populated with the results of calling a provided function on every item in the calling array.

let moneyheist = ["Tokyo", "Nairobi", "Berlin", "Oslo", "Moscow"]

let moneyheist_set = new Set()

moneyheist.map(el => moneyheist_set.add(el))

console.log(moneyheist_set)

Output

Set(5) { 'Tokyo', 'Nairobi', 'Berlin', 'Oslo', 'Moscow' }

Here, we are mapping through every element of the array and adding an element one by one to the set, and in the end, the set is formed with the array values.

Using Array.prototype.forEach() function

The array.forEach() is a built-in JavaScript function that individually adds each array element to the set object.

let moneyheist = ["Tokyo", "Nairobi", "Berlin", "Oslo", "Moscow"]

let moneyheist_set = new Set()

moneyheist.forEach(el => moneyheist_set.add(el))

console.log(moneyheist_set)

Output

Set(5) { 'Tokyo', 'Nairobi', 'Berlin', 'Oslo', 'Moscow' }

In this example, we are calling forEach() method to an array that takes a single element at a time and adds to the set and returns the transformed set.

Convert using Array.prototype.reduce() function

The array reduce() is a built-in JavaScript method that executes a reducer function for each value of an array. The reduce() returns a single value which is the function’s accumulated result.

let moneyheist = ["Tokyo", "Nairobi", "Berlin", "Oslo", "Moscow"]

let moneyheist_set = new Set()

moneyheist.reduce((_, e) => moneyheist_set.add(e), null)

console.log(moneyheist_set)

Output

Set(5) { 'Tokyo', 'Nairobi', 'Berlin', 'Oslo', 'Moscow' }

That’s it for this tutorial.

See also

JavaScript array to object

JavaScript array to json

JavaScript object to string

Leave a Comment