How To Convert An Array To A Set In JavaScript

How to Convert an Array to a Set in JavaScript

To convert an array to a set in JavaScript, we have tested effectively with four methods: Using the Set() constructor, the reduce() method, the forEach() method, and the map() method. Check out the details below to get more information.

Convert an array to a set in JavaScript

Using Set() constructor

The best solution to convert an array to a set is using the Set() constructor. It accepts an iterable object.

Syntax:

Set(iterable)

Parameters:

  • iterable: The new set is empty if you don’t specify this parameter.

Return value: A new set object.

Example:

var myArray = ["Welcome", "to", "to", "Learn", "Learn", "Share", "IT"];
console.log(myArray);

// Remove duplicate elements in the array
var convertToSet = new Set(myArray);
console.log("After converting my array to Set:");
console.log(convertToSet);

Output

["Welcome", "to", "to", "Learn", "Learn", "Share", "IT"]
After converting my array to Set:
{Welcome, to, Learn, Share, IT}

Using the reduce() method

You can convert an array to a set by using the reduce() method in the Javascript program.

Syntax:  

array.reduce(function(total, currentValue, currentIndex, arr), initialValue)

Parameters: 

  • function(): To run for each element in the array.
  • total: The initial value.
  • currentValue: The value of the current element.
  • currentIndex: The index of the current element.
  • arr: The array the current element belongs to.
  • initialValue: A value to be passed to the function. 

Return value: The accumulated result.

Example:

In this example, to transform an array into a set, we will use the reduce() method. The reduce() method will call a method to shorten every element of the array and return it as a single value.

var myArray = ["Welcome", "to", "to", "Learn", "Learn", "Share", "IT"];
console.log(myArray);
var set = new Set();

// Reduce duplicate element in the array and then add to the set
myArray.reduce((_, item) => set.add(item), null);
console.log("After converting my array to Set:");
console.log(set);

Output

["Welcome", "to", "to", "Learn", "Learn", "Share", "IT"]
After converting my array to Set:
{Welcome, to, Learn, Share, IT}

Using forEach() method

Another simple way is using the forEach() function to add each array element into a set. 

Syntax:  

array.forEach(function(currentValue, index, arr), thisValue)

Parameters: 

  • function(): To run for each element in array.
  • currentValue: The value of the current element.
  • currentIndex: The index of the current element.
  • arr: The array the current element belongs to.
  • thisValue: Default is undefined, a value passed to the function. 

Return value: Return value is undefined.

Example:

var myArray = ["Welcome", "to", "to", "Learn", "Learn", "Share", "IT"];
console.log(myArray);
var set = new Set();

// Add each array element into the set
myArray.forEach(item => set.add(item));
console.log("After converting my array to Set:");
console.log(set);

Output:

["Welcome", "to", "to", "Learn", "Learn", "Share", "IT"]
After converting my array to Set:
{Welcome, to, Learn, Share, IT}

Using the map() method

We can use the map() method to construct a new array without updating the current array and then use the add() method to add the array element to the set.

Syntax:  

array.map(function(currentValue, index, arr), thisValue)

Parameters: 

  • function(): To run for each element in the array.
  • currentValue: The value of the current element.
  • index: The index of the current element.
  • arr: The array the current element belongs to.
  • thisValue: Default is undefined, a value passed to the function. 

Return value: A new array.

Example:

var myArray = ["Welcome", "to", "to", "Learn", "Learn", "Share", "IT"];
console.log(myArray);
var set = new Set();

// Use the map() method will construct a new array and add an element to the set
myArray.map(item => set.add(item));
console.log("After converting my array to Set:");
console.log(set);

Output

["Welcome", "to", "to", "Learn", "Learn", "Share", "IT"]
After converting my array to Set:
{Welcome, to, Learn, Share, IT}

Summary

In this tutorial, we have explained how to convert an array to a set in JavaScript, the solution we recommend is using the Set() constructor. We always hope this tutorial is helpful to you. Leave your comment here if you have any questions or comments to improve the article. Thanks for your reading.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *