![]() |
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. Table of Content Converting Map keys into SetYou 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
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 SetThe 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
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 SetA 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
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 syntaxThe 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
Output
Initial Map: Map(2) { 'name1' => 'GeeksforGeeks', 'name2' => 'Virat Kohli' } Created Set: Set(2) { [ 'name1', 'GeeksforGeeks' ], [ 'name2', 'Virat Kohli' ] } Using the Array.from() methodThe 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
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 |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 19 |