How to convert Map keys to an array in JavaScript ?
Given a Map and the task is to get the keys of the map into an array using JavaScript.
Approach 1:
- Declare new map object
- Display the map content
- Use keys() method on Map Object to get the keys of Map.
- Then use the array.from() method to convert map object to array.
Example 1: This example uses array.from() method to get the keys of the Map in the array.
<!DOCTYPE HTML> <html> <head> <title> How to convert Map keys to array in JavaScript ? </title> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksForGeeks </h1> <p id = "GFG_UP" style = "font-size: 19px; font-weight: bold;"> </p> <button onClick = "GFG_Fun()"> click here </button> <p id = "GFG_DOWN" style = "color: green; font-size: 24px; font-weight: bold;"> </p> <script> var up = document.getElementById('GFG_UP'); var down = document.getElementById('GFG_DOWN'); let myMap = new Map().set('GFG', 1).set('Geeks', 2); up.innerHTML = 'myMap = ("GFG" => 1, "Geeks" => 2)'; function GFG_Fun() { var array = Array.from(myMap.keys()); down.innerHTML = "Elements in array are " + array + " and Length of array = " + array.length; } </script> </body> </html> |
chevron_right
filter_none
Output:
-
Before clicking on the button:
-
After clicking on the button:
Approach 2:
- Declare new map object
- Display the map content
- Use [ …myMap.keys() ] method on Map Object to get the keys of Map as an array.
Example 2: This example uses the [ …Map.keys() ] method to get the keys of the Map in the array.
<!DOCTYPE HTML> <html> <head> <title> How to convert Map keys to array in JavaScript </title> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksForGeeks </h1> <p id = "GFG_UP" style = "font-size: 19px; font-weight: bold;"> </p> <button onClick = "GFG_Fun()"> click here </button> <p id = "GFG_DOWN" style = "color: green; font-size: 24px; font-weight: bold;"> </p> <script> var up = document.getElementById('GFG_UP'); var down = document.getElementById('GFG_DOWN'); let myMap = new Map().set('GFG', 1).set('Geeks', 2); up.innerHTML = 'myMap = ("GFG" => 1, "Geeks" => 2)'; function GFG_Fun() { var array = [ ...myMap.keys() ]; down.innerHTML = "Elements in array are " + array + " and Length of array = " + array.length; } </script> </body> </html> |
chevron_right
filter_none
Output:
-
Before clicking on the button:
-
After clicking on the button:
Recommended Posts:
- JavaScript | array.keys()
- How to get a list of associative array keys in JavaScript ?
- How to Convert Array to Set in JavaScript?
- How to convert Set to Array in JavaScript?
- Convert an Array to an Object in JavaScript
- JavaScript | Convert an array to JSON
- How to convert PHP array to JavaScript or JSON ?
- How to convert arguments object into an array in JavaScript ?
- Convert comma separated string to array using JavaScript
- Object.keys( ) In JavaScript
- JavaScript | typedArray.keys() with Example
- Reset keys of array elements using PHP ?
- How to convert decimal to hex in JavaScript ?
- How to convert a JavaScript date to UTC?
- Convert string into date using JavaScript
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



