Convert a Set to a Map in JavaScript
This post will discuss how to convert a set to a map in JavaScript.
Converting a set to a map means creating a new map that contains the elements of the set as keys and some values associated with them. There are several methods to convert a set to a map in JavaScript, depending on what we want to use as the keys and values of the map. Here are some of the methods we can use:
1. Using Array.from() function
We can use the Array.from() function to convert a set into an array of key-value pairs, and then pass that array to the Map() constructor. We can provide a mapping function as the second argument to the Array.from() function to generate the values for each key. The following code illustrates this:
|
1 2 3 4 5 6 7 8 9 10 11 |
// Create a set of numbers let set = new Set([1, 2, 3, 4, 5]); // Convert the set to an array and map each element to its square let array = Array.from(set, x => [x, x * x]); // Convert the array to a map let map = new Map(array); // The map contains the numbers as keys and their squares as values console.log(map); // Map(5) { 1 => 1, 2 => 4, 3 => 9, 4 => 16, 5 => 25 } |
2. Using Array.map() function
We can also use the spread operator (…) to spread elements of a set into an array, and then uses the Array.map() function to create an array of key-value pairs. We can provide a mapping function as the argument to the Array.map() function to generate the values for each key. Finally, we pass the array of key-value pairs to the Map() constructor. The following code illustrates this:
|
1 2 3 4 5 6 7 8 9 10 11 |
// Create a set of strings let set = new Set(["a", "b", "c", "d", "e"]); // Spread the set into an array and map each element to its upper case let array = [...set].map(x => [x, x.toUpperCase()]); // Convert the array to a map let map = new Map(array); // The map contains the strings as keys and their upper cases as values console.log(map); // Map(5) { "a" => "A", "b" => "B", "c" => "C", "d" => "D", "e" => "E" } |
3. Using Set.forEach() function
The Set.forEach() is another way to iterate over the elements of a set and add them as key-value pairs to a new map using the Map.set() function. This can be used to convert any type of set into a map by providing a function that returns the key and the value for each element. The following code illustrates this:
|
1 2 3 4 5 6 7 8 9 10 11 |
// Create a set of numbers let set = new Set([1, 2, 3, 4, 5]); // create an empty map let map = new Map(); // Convert set to a map set.forEach(x => map.set(x, x * 2)); // The map contains the numbers as keys and their double as values console.log(map); // Map(5) { 1 => 2, 2 => 4, 3 => 6, 4 => 8, 5 => 10 } |
4. Using for…of loop
This iterates over the elements of a set with a for…of loop and adds them as keys to a new map with the Map.set() function. We can use any logic inside the loop to generate the values for each key. The following code illustrates this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// Create a set of booleans let set = new Set([true, false]); // Create an empty map let map = new Map(); // Iterate over the set for (let element of set) { // Use a ternary operator to generate the values let value = element ? "Yes" : "No"; // Add the key-value pair to the map map.set(element, value); } // The map contains the booleans as keys and their string representations as values console.log(map); // Map(2) { true => "Yes", false => "No" } |
That’s all about converting a set to a map in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)