Convert An Array To A Set In JavaScript

To convert an array to a Set in JavaScript, you can use any of these two methods –

  1. Pass the array as a parameter to the Set() constructor. The Set() constructor will convert the array to a set of unique values from the array.
  2. Use a forEach() loop and add each element of the array to a Set. The set will store all the unique values from the array.
  3. Use a for loop and add each element of the array to a Set. The set will store all the unique values from the array.

Let’s discuss each of these methods in detail below.

Convert An Array To A Set In JavaScript

convert an array to a set in javascript

Use The Set() Constructor

You can pass the array as an argument to the Set() constructor. The Set() constructor will convert the array to a set of unique values from the array. The unique values can be numbers, strings, or objects.

Consider the following example:

let myArray = [‘a’, ‘b’, ‘c’, ‘a’, ‘b’, ‘c’];

let mySet = new Set(myArray);

console.log(mySet); // Set { ‘a’, ‘b’, ‘c’ }

As you can see from the above example, the mySet set contains only unique values from the myArray array.

You can convert the set back to an array of unique values by using either the Array.from() method or by using the spread operator (…) as follows:

let mySet = new Set(myArray);

console.log(mySet); // Set { ‘a’, ‘b’, ‘c’ }

// using the Array.from() method

console.log(Array.from(mySet)); // [‘a’, ‘b’, ‘c’]

// using the spread operator (...)

console.log([...mySet]); // [‘a’, ‘b’, ‘c’]

Use A forEach() Loop

You can use a forEach() loop and add each element of the array to a Set. The set will store all the unique values from the array. Consider the following example:

let myArray = [‘a’, ‘b’, ‘c’, ‘a’, ‘b’, ‘c’];

let mySet = new Set();

myArray.forEach(function(value) {

  mySet.add(value);

});

console.log(mySet); // Set { ‘a’, ‘b’, ‘c’ }

You can convert the set back to an array of unique values by using either the Array.from() method or by using the spread operator (…) as follows:

let myArray = [‘a’, ‘b’, ‘c’, ‘a’, ‘b’, ‘c’];

let mySet = new Set();

myArray.forEach(function(value) {

  mySet.add(value);

});

console.log(mySet); // Set { ‘a’, ‘b’, ‘c’ }

// using the Array.from() method

console.log(Array.from(mySet)); // [‘a’, ‘b’, ‘c’]

// using the spread operator (...)

console.log([...mySet]); // [‘a’, ‘b’, ‘c’]

Use A for Loop

You can use a for loop and add each element of the array to a Set. The set will store all the unique values from the array. Consider the following example:

let myArray = [‘a’, ‘b’, ‘c’, ‘a’, ‘b’, ‘c’];

let mySet = new Set();

for (let i = 0; i < myArray.length; i++) {

  mySet.add(myArray[i]);

}

console.log(mySet); // Set { ‘a’, ‘b’, ‘c’ }

You can convert the set back to an array of unique values by using either the Array.from() method or by using the spread operator (…) as follows:

let myArray = [‘a’, ‘b’, ‘c’, ‘a’, ‘b’, ‘c’];

let mySet = new Set();

for (let i = 0; i < myArray.length; i++) {

  mySet.add(myArray[i]);

}

console.log(mySet); // Set { ‘a’, ‘b’, ‘c’ }

// using the Array.from() method

console.log(Array.from(mySet)); // [‘a’, ‘b’, ‘c’]

// using the spread operator (…)

console.log([...mySet]); // [‘a’, ‘b’, ‘c’]

Conclusion

In this article, you learned how to convert an array to a Set in JavaScript. You can use any of the three methods discussed above to achieve this. I hope this article was helpful. Thank you for reading! 🙂

Leave a Reply