How to convert a Set to Map in JavaScript?
Last Updated :
22 Jan, 2024
A Set is a collection of unique values. It stores only the data in the form of values while a Map can store the data in the form of key-value pairs.
These are the different ways of converting a Set to a Map in JavaScript:
Using the Array.from() method
The Array.from() method can be used to convert a Set to a Map by passing the Map and a callback function to set the keys and the values of the Map as parameters to this method.
Syntax:
Array.from(setName, ()=>{});
Example: The below code example uses the Array.from() method to convert a Set to a Map.
JavaScript
const dummySet = new Set();
dummySet.add('GeeksforGeeks');
dummySet.add('Virat Kohli');
console.log("Initial Set: ", dummySet);
const createdMap = new Map(Array.from
(dummySet, value => [value.length, value]));
console.log("Created Map: ", createdMap);
OutputInitial Set: Set(2) { 'GeeksforGeeks', 'Virat Kohli' }
Created Map: Map(2) { 13 => 'GeeksforGeeks', 11 => 'Virat Kohli' }
Using the Spread Operator with map() method
The spread operator is used to spread the data of the Set and the the map() method will be used to iterate over them to store them in an empty Map.
Syntax:
[...setName].map(()=>{});
Example: The below code is the practical implementation of the above approach to convert a Set into a Map.
JavaScript
const dummySet = new Set();
dummySet.add('GeeksforGeeks');
dummySet.add('Virat Kohli');
console.log("Initial Set: ", dummySet);
const createdMap = new Map([...dummySet].
map((item)=>[item.length, item]));
console.log("Created Map: ", createdMap);
OutputInitial Set: Set(2) { 'GeeksforGeeks', 'Virat Kohli' }
Created Map: Map(2) { 13 => 'GeeksforGeeks', 11 => 'Virat Kohli' }
Using the reduce() method
The JavaScript reduce() method can be used to convert the Set to a Map when it is used with the spread operator.
Syntax:
[...setName].reduce(()=>{});
Example: The below code shows the practical implementation of the reduce() method to convert a Set to a Map.
JavaScript
const dummySet = new Set();
dummySet.add('GeeksforGeeks');
dummySet.add('Virat Kohli');
console.log("Initial Set: ", dummySet);
const createdMap = [...dummySet].
reduce((map, item)=>
map.set(item.length, item), new Map());
console.log("Created Map: ", createdMap);
OutputInitial Set: Set(2) { 'GeeksforGeeks', 'Virat Kohli' }
Created Map: Map(2) { 13 => 'GeeksforGeeks', 11 => 'Virat Kohli' }
Using the forEach() method
The forEach() method can be used to iterate through each element of the Set to set them as key-value pair of the Map one by one.
Syntax:
setName.forEach(()=>{});
Example: The below code can be used to convert a Set to a Map using the forEach() method.
JavaScript
const dummySet = new Set();
dummySet.add('GeeksforGeeks');
dummySet.add('Virat Kohli');
console.log("Initial Set: ", dummySet);
const createdMap = new Map();
dummySet.forEach((item) =>
createdMap.set(item.length, item));
console.log("Created Map: ", createdMap);
OutputInitial Set: Set(2) { 'GeeksforGeeks', 'Virat Kohli' }
Created Map: Map(2) { 13 => 'GeeksforGeeks', 11 => 'Virat Kohli' }
Using the for of loop
The for of loop can be used to get the items of the Set one by one and then set them as the key value pairs of the Map using the Map set() Method.
Syntax:
for(const itemName of setName){}
Example: The below code implements the for of loop to convet a Set to a Map in JavaScript.
JavaScript
const dummySet = new Set();
dummySet.add('GeeksforGeeks');
dummySet.add('Virat Kohli');
console.log("Initial Set: ", dummySet);
const createdMap = new Map();
for(const item of dummySet){
createdMap.set(item.length, item);
}
console.log("Created Map: ", createdMap);
OutputInitial Set: Set(2) { 'GeeksforGeeks', 'Virat Kohli' }
Created Map: Map(2) { 13 => 'GeeksforGeeks', 11 => 'Virat Kohli' }
Similar Reads
How to Convert Array to Set in JavaScript?
The Set constructor is used to convert the array into set in JavaScript. It is useful to clean the array and remove duplicate elements. Converting Array to Set means transforming the array elements into a Set object.Using Set ConstructorThe Set constructor in JavaScript directly converts an array in
2 min read
How to Convert a Map to JSON String in JavaScript ?
A Map is a collection of key-value pairs, where each key is unique. In this article, we will see how to convert a Map to a JSON (JavaScript Object Notation) string in JavaScript. However, JSON.stringify() does not directly support Map objects. Table of ContentUsing Object.fromEntries() MethodUsing A
2 min read
How to convert a Map into a Set in JavaScript?
Map and Set in JavaScript are special kind of data structures that holds only the unique data. There will be no duplicate data stored in them. Maps store data in the form of key-value pairs, while the Sets store in the form of values only. In some scenarios, you need to convert a Map into a Set, the
4 min read
How to Convert Map to JSON in JavaScript ?
In JavaScript, when working with data, you might encounter situations where you need to convert a Map object into a JSON format. This can be useful for sending data over the network, storing data in local storage, or interfacing with APIs that expect JSON data. Converting a Map to JSON means convert
3 min read
How to Convert Object to Array in JavaScript?
In this article, we will learn how to convert an Object to an Array in JavaScript. Given an object, the task is to convert an object to an Array in JavaScript. Objects and Arrays are two fundamental data structures. Sometimes, it's necessary to convert an object to an array for various reasons, such
4 min read
How to convert a map to array of objects in JavaScript?
A map in JavaScript is a set of unique key and value pairs that can hold multiple values with only a single occurrence. Sometimes, you may be required to convert a map into an array of objects that contains the key-value pairs of the map as the values of the object keys. Let us discuss some methods
6 min read
How to Convert an Array of Objects to Map in JavaScript?
Here are the different methods to convert an array of objects into a Map in JavaScript1. Using new Map() ConstructorThe Map constructor can directly create a Map from an array of key-value pairs. If each object in the array has a specific key-value structure, you can map it accordingly.JavaScriptcon
3 min read
How To Convert Map Keys to an Array in JavaScript?
Here are the various methods to convert Map keys to an array in JavaScript1. Using array.from() MethodThe Array.from() method in JavaScript converts Map keys to an array by using 'Array.from(map.keys())'. JavaScriptlet map = new Map().set('GFG', 1).set('Geeks', 2); let a = Array.from(map.keys()); co
2 min read
How to Sort a Map in JavaScript?
Sorting a Map in JavaScript involves ordering its key-value pairs based on the keys or values. Since Maps maintain the insertion order, you can't directly sort them like arrays. Instead, you'll need to convert the Map into an array, sort it, and then convert it back into a Map.Below are the approach
3 min read
How to sort a set in JavaScript ?
Sorting a Set based on the values of its elements is a common need in JavaScript. While the built-in sort() function can't be directly applied to a Set, there's a workaround. Let's explore how we can achieve this: Instead of directly sorting the Set, we'll: Convert the Set into an array to make it s
3 min read