The task is to convert a JavaScript Object into plain ES6 Map using JavaScript. we’re going to discuss few techniques.
Approach
- Create an object.
- Create a new map.
- Pass the object to the map and set its properties.
Example 1:
<!DOCTYPE HTML> <html> <head> <title> How to convert a plain object into ES6 Map using JavaScript ? </title> </head> <body style="text-align:center;"> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style= "font-size: 15px; font-weight: bold;"> </p> <button onclick="GFG_Fun()"> click here </button> <p id="GFG_DOWN" style= "color:green; font-size: 20px; font-weight: bold;"> </p> <script> var up = document.getElementById('GFG_UP'); up.innerHTML = "Click on the button to "+ "convert the JavaScript Object into map."; var down = document.getElementById('GFG_DOWN'); var obj = { prop_1: 'val_1', prop_2: 'val_2', prop_3: 'val_3' }; var heading = document.getElementById('h1'); function GFG_Fun() { const map = new Map(Object.entries(obj)); down.innerHTML = "Val of prop_2 is " + map.get('prop_2'); } </script> </body> </html> |
chevron_right
filter_none
Output:
-
Before clicking on the button:
-
After clicking on the button:
Example 2:
<!DOCTYPE HTML> <html> <head> <title> How to convert a plain object into ES6 Map using JavaScript ? </title> </head> <body style="text-align:center;"> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style= "font-size: 15px; font-weight: bold;"> </p> <button onclick="GFG_Fun()"> click here </button> <p id="GFG_DOWN" style= "color:green; font-size: 20px; font-weight: bold;"> </p> <script> var up = document.getElementById('GFG_UP'); up.innerHTML = "Click on the button to "+ "convert the JavaScript Object into map."; var down = document.getElementById('GFG_DOWN'); var obj = { prop_1: 'val_1', prop_2: 'val_2', prop_3: 'val_3' }; function createMap(obj) { let map = new Map(); Object.keys(obj).forEach(key => { map.set(key, obj[key]); }); return map; } function GFG_Fun() { const map = createMap(obj); down.innerHTML = "Val of prop_1 is " + map.get('prop_1'); } </script> </body> </html> |
chevron_right
filter_none
Output:
-
Before clicking on the button:
-
After clicking on the button:
Recommended Posts:
- How to replace plain URL with link using JavaScript ?
- Convert URL parameters to a JavaScript Object
- Convert an Array to an Object in JavaScript
- How to convert an object to string using JavaScript?
- How to convert arguments object into an array in JavaScript ?
- How to convert an Object {} to an Array [] of key-value pairs in JavaScript?
- How to Convert JS Object to JSON String in JQuery/Javascript?
- How to convert Object's array to an array using JavaScript ?
- What is the difference between Object.keys() and Object.entries() methods in JavaScript ?
- How to access an object having spaces in the object's key using JavaScript ?
- How to check a JavaScript Object is a DOM Object ?
- How to convert an array into object using stdClass() in PHP?
- Convert an object to associative array in PHP
- How to display HTML tags as plain text using PHP
- How to get the first key name of a JavaScript object ?
- Map vs Object in JavaScript
- How to get a key in a JavaScript object by its value ?
- Object.is( ) in JavaScript
- How to clone a JavaScript object?
- JavaScript | Object Constructors
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.


