Horje
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, 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);

Output

Initial 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);

Output

Initial 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);

Output

Initial 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);

Output

Initial 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);

Output

Initial Map:  Map(2) { 'name1' => 'GeeksforGeeks', 'name2' => 'Virat Kohli' }
Created Set:  Set(2) { [ 'name1', 'GeeksforGeeks' ], [ 'name2', 'Virat Kohli' ] }




Reffered: https://www.geeksforgeeks.org


JavaScript

Related
How to convert a Set to Map in JavaScript? How to convert a Set to Map in JavaScript?
How Dynamic Arrays Work in JavaScript ? How Dynamic Arrays Work in JavaScript ?
How to Check if a Value is a Number in JavaScript ? How to Check if a Value is a Number in JavaScript ?
How to Make an SVG Image in JavaScript ? How to Make an SVG Image in JavaScript ?
How to Sum all Numbers in a Range in JavaScript ? How to Sum all Numbers in a Range in JavaScript ?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
19