
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Object to Map conversion in JavaScript
Suppose, we have an object like this −
const obj = { name: "Jai", age: 32, occupation: "Software Engineer", address: "Dhindosh, Maharasthra", salary: "146000" };
We are required to write a JavaScript function that takes in such an object with key value pairs and converts it into a Map.
Example
The code for this will be −
const obj = { name: "Jai", age: 32, occupation: "Software Engineer", address: "Dhindosh, Maharasthra", salary: "146000" }; const objectToMap = obj => { const keys = Object.keys(obj); const map = new Map(); for(let i = 0; i < keys.length; i++){ //inserting new key value pair inside map map.set(keys[i], obj[keys[i]]); }; return map; }; console.log(objectToMap(obj));
Output
The output in the console −
Map(5) { 'name' => 'Jai', 'age' => 32, 'occupation' => 'Software Engineer', 'address' => 'Dhindosh, Maharasthra', 'salary' => '146000' }
- Related Articles
- Map object in JavaScript.
- Convert object to a Map - JavaScript
- JavaScript map value to keys (reverse object mapping)
- Javascript Map vs Object — What and when?
- Convert 2D array to object using map or reduce in JavaScript
- What is the use of map object in javascript?
- How to convert a plain object into ES6 Map using JavaScript?
- How to use my object like an array using map function in JavaScript?
- Map numbers to characters in JavaScript
- How to Map IntSteam to String object in Java
- Binary to original string conversion in JavaScript
- HTML DOM Map Object
- Map anagrams to one another in JavaScript
- DNA to RNA conversion using JavaScript
- Decimal to binary conversion using recursion in JavaScript

Advertisements