How to convert a Map into a Set in JavaScript?
Last Updated :
22 Jan, 2024
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, there you can use the below methods for the conversion.
Converting Map keys into Set
You can create a Set that contains the keys of the Map as values using the keys() method on the Map to get its keys and store them as values of the Set.
Syntax:
mapName.keys();
Example: The below code explains the practical use of the keys() method to convert the keys of a Map into a Set.
JavaScript
const dummyMap = new Map();
dummyMap.set('type1', 'Company');
dummyMap.set('name1', 'GeeksforGeeks');
dummyMap.set('type2', 'Cricketer');
dummyMap.set('name2', 'Virat Kohli');
console.log("Initial Map: ", dummyMap);
const createdSet = new Set(dummyMap.keys());
console.log("Created Keys Set: ", createdSet);
OutputInitial Map: Map(4) {
'type1' => 'Company',
'name1' => 'GeeksforGeeks',
'type2' => 'Cricketer',
'name2' => 'Virat Kohli'
}
Created Keys Set: Set(4) { 'type1', 'name1', 'type2', 'name2' }
Converting Map values into Set
The values() method can be used with the Map to get its values and store them into a Set as its values. This method will convert the Map values into a Set.
Syntax:
mapName.values();
Example: The below code implements the values() method with a Map to convert its values into the Set values.
JavaScript
const dummyMap = new Map();
dummyMap.set('type1', 'Company');
dummyMap.set('name1', 'GeeksforGeeks');
dummyMap.set('type2', 'Cricketer');
dummyMap.set('name2', 'Virat Kohli');
console.log("Initial Map: ", dummyMap);
const createdSet = new Set(dummyMap.values());
console.log("Created Values Set: ", createdSet);
OutputInitial Map: Map(4) {
'type1' => 'Company',
'name1' => 'GeeksforGeeks',
'type2' => 'Cricketer',
'name2' => 'Virat Kohli'
}
Created Values Set: Set(4) { 'Company', 'GeeksforGeeks', 'Cricketer...
Converting Keys and Values of a Map into Set
A Set that contains both the keys and values of the Map as values can also be created using the entries() method to get the Map keys and values and store them into a Set.
Syntax:
mapName.entries();
Example: The below code is the practical implementation of the entries() method to convert a Map into a Set.
JavaScript
const dummyMap = new Map();
dummyMap.set('name1', 'GeeksforGeeks');
dummyMap.set('name2', 'Virat Kohli');
console.log("Initial Map: ", dummyMap);
const createdSet = new Set(dummyMap.entries());
console.log
("Created Set of Keys and Values: \n", createdSet);
OutputInitial Map: Map(2) { 'name1' => 'GeeksforGeeks', 'name2' => 'Virat Kohli' }
Created Set of Keys and Values:
Set(2) { [ 'name1', 'GeeksforGeeks' ], [ 'name2', 'Virat Kohli' ] }
Using the spread operator syntax
The spread operator can be used to destructure the key and the value pairs of the Map which then can be stored to the Set as its values.
Syntax:
[...mapName];
Example: The below code uses the spread operator to convert a Map into a Set.
JavaScript
const dummyMap = new Map();
dummyMap.set('name1', 'GeeksforGeeks');
dummyMap.set('name2', 'Virat Kohli');
console.log("Initial Map: ", dummyMap);
const createdSet = new Set([...dummyMap]);
console.log("Created Set: ", createdSet);
OutputInitial Map: Map(2) { 'name1' => 'GeeksforGeeks', 'name2' => 'Virat Kohli' }
Created Set: Set(2) { [ 'name1', 'GeeksforGeeks' ], [ 'name2', 'Virat Kohli' ] }
Using the Array.from() method
The Array.from() method can also be used to convert a Map into a Set by passing the Map as parameter to it.
Syntax:
Array.from(mapName);
Example: The below code implements the Array.from() method to convert a Map into a Set.
JavaScript
const dummyMap = new Map();
dummyMap.set('name1', 'GeeksforGeeks');
dummyMap.set('name2', 'Virat Kohli');
console.log("Initial Map: ", dummyMap);
const createdSet =
new Set(Array.from(dummyMap));
console.log("Created Set: ", createdSet);
OutputInitial Map: Map(2) { 'name1' => 'GeeksforGeeks', 'name2' => 'Virat Kohli' }
Created Set: Set(2) { [ 'name1', 'GeeksforGeeks' ], [ 'name2', 'Virat Kohli' ] }
Similar Reads
How to convert a Set to Map in JavaScript?
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: Table of Content Using the Array.from() methodUsing the Spread Operator with m
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 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 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 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 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 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 an Object into Array of Objects in JavaScript?
Here are the different methods to convert an object into an array of objects in JavaScript1. Using Object.values() methodObject.values() method extracts the property values of an object and returns them as an array, converting the original object into an array of objects.JavaScriptconst a = { java:
3 min read
How to convert arguments object into an array in JavaScript ?
The arguments object is an array-like object that represents the arguments passed in when invoking a function. This array-like object does not have the array prototype chain, hence it cannot use any of the array methods. This object can be converted into a proper array using two approaches: Method 1
5 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