There is no native map to the Object object, but how about this:

var myObject = { 'a': 1, 'b': 2, 'c': 3 };

Object.keys(myObject).forEach(function(key, index) {
  myObject[key] *= 2;
});

console.log(myObject);
// => { 'a': 2, 'b': 4, 'c': 6 }

But you could easily iterate over an object using for ... in:

var myObject = { 'a': 1, 'b': 2, 'c': 3 };

for (var key in myObject) {
  if (myObject.hasOwnProperty(key)) {
    myObject[key] *= 2;
  }
}

console.log(myObject);
// { 'a': 2, 'b': 4, 'c': 6 }

Update

A lot of people are mentioning that the previous methods do not return a new object, but rather operate on the object itself. For that matter I wanted to add another solution that returns a new object and leaves the original object as it is:

var myObject = { 'a': 1, 'b': 2, 'c': 3 };

// returns a new object with the values at each key mapped using mapFn(value)
function objectMap(object, mapFn) {
  return Object.keys(object).reduce(function(result, key) {
    result[key] = mapFn(object[key])
    return result
  }, {})
}

var newObject = objectMap(myObject, function(value) {
  return value * 2
})

console.log(newObject);
// => { 'a': 2, 'b': 4, 'c': 6 }

console.log(myObject);
// => { 'a': 1, 'b': 2, 'c': 3 }

Array.prototype.reduce reduces an array to a single value by somewhat merging the previous value with the current. The chain is initialized by an empty object {}. On every iteration a new key of myObject is added with twice the key as the value.

Update

With new ES6 features, there is a more elegant way to express objectMap.

const objectMap = (obj, fn) =>
  Object.fromEntries(
    Object.entries(obj).map(
      ([k, v], i) => [k, fn(v, k, i)]
    )
  )
  
const myObject = { a: 1, b: 2, c: 3 }

console.log(objectMap(myObject, v => 2 * v)) 

Answer from Amberlamps on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Map
Map - JavaScript - MDN Web Docs - Mozilla
February 16, 2026 - Map objects are collections of key-value pairs. A key in the Map may only occur once; it is unique in the Map's collection. A Map object is iterated by key-value pairs — a for...of loop returns a 2-member array of [key, value] for each iteration.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Map › forEach
Map.prototype.forEach() - JavaScript - MDN Web Docs
function logMapElements(value, key, map) { console.log(`map.get('${key}') = ${value}`); } new Map([ ["foo", 3], ["bar", {}], ["baz", undefined], ]).forEach(logMapElements); // Logs: // "map.get('foo') = 3" // "map.get('bar') = [object Object]" // "map.get('baz') = undefined"
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › data types
Map and Set
And the standard iteration for map returns same key/value pairs as map.entries(). So we get a plain object with same key/values as the map. A Set is a special type collection – “set of values” (without keys), where each value may occur only once. ... new Set([iterable]) – creates the set, and if an iterable object is provided (usually an array), copies values from it into the set.
Top answer
1 of 16
2603

There is no native map to the Object object, but how about this:

var myObject = { 'a': 1, 'b': 2, 'c': 3 };

Object.keys(myObject).forEach(function(key, index) {
  myObject[key] *= 2;
});

console.log(myObject);
// => { 'a': 2, 'b': 4, 'c': 6 }

But you could easily iterate over an object using for ... in:

var myObject = { 'a': 1, 'b': 2, 'c': 3 };

for (var key in myObject) {
  if (myObject.hasOwnProperty(key)) {
    myObject[key] *= 2;
  }
}

console.log(myObject);
// { 'a': 2, 'b': 4, 'c': 6 }

Update

A lot of people are mentioning that the previous methods do not return a new object, but rather operate on the object itself. For that matter I wanted to add another solution that returns a new object and leaves the original object as it is:

var myObject = { 'a': 1, 'b': 2, 'c': 3 };

// returns a new object with the values at each key mapped using mapFn(value)
function objectMap(object, mapFn) {
  return Object.keys(object).reduce(function(result, key) {
    result[key] = mapFn(object[key])
    return result
  }, {})
}

var newObject = objectMap(myObject, function(value) {
  return value * 2
})

console.log(newObject);
// => { 'a': 2, 'b': 4, 'c': 6 }

console.log(myObject);
// => { 'a': 1, 'b': 2, 'c': 3 }

Array.prototype.reduce reduces an array to a single value by somewhat merging the previous value with the current. The chain is initialized by an empty object {}. On every iteration a new key of myObject is added with twice the key as the value.

Update

With new ES6 features, there is a more elegant way to express objectMap.

const objectMap = (obj, fn) =>
  Object.fromEntries(
    Object.entries(obj).map(
      ([k, v], i) => [k, fn(v, k, i)]
    )
  )
  
const myObject = { a: 1, b: 2, c: 3 }

console.log(objectMap(myObject, v => 2 * v)) 

2 of 16
618

How about a one-liner in JS ES10 / ES2019 ?

Making use of Object.entries() and Object.fromEntries():

let newObj = Object.fromEntries(Object.entries(obj).map(([k, v]) => [k, v * v]));

The same thing written as a function:

function objMap(obj, func) {
  return Object.fromEntries(Object.entries(obj).map(([k, v]) => [k, func(v)]));
}

// To square each value you can call it like this:
let mappedObj = objMap(obj, (x) => x * x);

This function uses recursion to square nested objects as well:

function objMap(obj, func) {
  return Object.fromEntries(
    Object.entries(obj).map(([k, v]) => 
      [k, v === Object(v) ? objMap(v, func) : func(v)]
    )
  );
}

// To square each value you can call it like this:
let mappedObj = objMap(obj, (x) => x * x);

With ES7 / ES2016 you can't use Objects.fromEntries, but you can achieve the same using Object.assign in combination with spread operators and computed key names syntax:

let newObj = Object.assign({}, ...Object.entries(obj).map(([k, v]) => ({[k]: v * v})));

ES6 / ES2015 Doesn't allow Object.entries, but you could use Object.keys instead:

let newObj = Object.assign({}, ...Object.keys(obj).map(k => ({[k]: obj[k] * obj[k]})));

ES6 also introduced for...of loops, which allow a more imperative style:

let newObj = {}

for (let [k, v] of Object.entries(obj)) {
  newObj[k] = v * v;
}


array.reduce()

Instead of Object.fromEntries and Object.assign you can also use reduce for this:

let newObj = Object.entries(obj).reduce((p, [k, v]) => ({ ...p, [k]: v * v }), {});


Inherited properties and the prototype chain:

In some rare situation you may need to map a class-like object which holds properties of an inherited object on its prototype-chain. In such cases Object.keys() and Object.entries() won't work, because these functions do not include the prototype chain.

If you need to map inherited properties, you can use for (key in myObj) {...}.

Here is an example of such situation:

const obj1 = { 'a': 1, 'b': 2, 'c': 3}
const obj2 = Object.create(obj1);  // One of multiple ways to inherit an object in JS.

// Here you see how the properties of obj1 sit on the 'prototype' of obj2
console.log(obj2)  // Prints: obj2.__proto__ = { 'a': 1, 'b': 2, 'c': 3}

console.log(Object.keys(obj2));  // Prints: an empty Array.
console.log(Object.entries(obj2));  // Prints: an empty Array.

for (let key in obj2) {
  console.log(key);              // Prints: 'a', 'b', 'c'
}

However, please do me a favor and avoid inheritance. :-)

🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Map › get
Map.prototype.get() - JavaScript - MDN Web Docs
The get() method of Map instances returns the value corresponding to the key in this Map, or undefined if there is none. Object values are returned as the same reference that was originally stored, not as a copy, so mutations to the returned object will be reflected anywhere that reference ...
🌐
Sentry
sentry.io › sentry answers › javascript › map function for objects (instead of arrays)
Map function for objects (instead of arrays) - JavaScript
Suppose you want to map over the ... and return a new array, leaving the original array unchanged. JavaScript does not have a map function for objects....
Find elsewhere
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › map
Array.prototype.map() - JavaScript - MDN Web Docs
The map() method of Array instances creates a new array populated with the results of calling a provided function on every element in the calling array.
🌐
freeCodeCamp
freecodecamp.org › news › javascript-map-and-set-objects-explained
How to Use the JavaScript Map and Set Objects – Explained with Code Examples
February 21, 2024 - It can use any type of data as the key value, while an object can only use string values as keys. Under the hood, the Map object performs better when you need to add and remove keys, so you might consider using it when your data changes frequently.
🌐
Zhenghao
zhenghao.io › posts › object-vs-map
When You Should Prefer Map Over Object In JavaScript
June 18, 2022 - In JavaScript, objects are handy. They allow us to easily group multiple pieces of data together. After ES6, we got a new addition to the language - Map. In a lot of aspects, it seems like a more capable Object with a somewhat clumsy interface. However, most people still reach for objects when they need a hash map and only switch to using Map when they realize the keys can't just be strings for their use cases.
🌐
W3Schools
w3schools.com › js › js_maps.asp
JavaScript Maps
JS Examples JS HTML DOM JS HTML ... JS Bootcamp JS Certificate JS Reference ... A JavaScript Map is an object that can store collections of key-value pairs, similar to a dictionary in other programming languages....
🌐
DigitalOcean
digitalocean.com › community › tutorials › understanding-map-and-set-objects-in-javascript
Understanding Map and Set Objects in JavaScript | DigitalOcean
August 27, 2021 - In this article, you learned that a Map is a collection of ordered key/value pairs, and that a Set is a collection of unique values. Both of these data structures add additional capabilities to JavaScript and simplify common tasks such as finding the length of a key/value pair collection and removing duplicate items from a data set, respectively. On the other hand, Objects and Arrays have been traditionally used for data storage and manipulation in JavaScript, and have direct compatibility with JSON, which continues to make them the most essential data structures, especially for working with REST APIs.
🌐
TutorialsPoint
tutorialspoint.com › javascript › javascript_maps_object.htm
JavaScript - The Maps Object
A Map object in JavaScript is a collection of key-value pairs where the keys can be of any type, including objects or functions. The order of the map elements is the same as the insertion order of the key-value pairs.
🌐
W3Schools
w3schools.com › js › js_map_methods.asp
JavaScript Map Methods
Remember: The key is an object (apples), not a string ("apples"): fruits.get("apples"); // Returns undefined Try it Yourself » · ES2024 added the Map.groupBy() method to JavaScript. The Map.groupBy() method groups elements of an object according to string values returned from a callback function.
🌐
W3Schools
w3schools.com › jsref › jsref_map.asp
JavaScript Array map() Method
alignContent alignItems alignSelf animation animationDelay animationDirection animationDuration animationFillMode animationIterationCount animationName animationTimingFunction animationPlayState background backgroundAttachment backgroundClip backgroundColor backgroundImage backgroundOrigin backgroundPosition backgroundRepeat backgroundSize backfaceVisibility border borderBottom borderBottomColor borderBottomLeftRadius borderBottomRightRadius borderBottomStyle borderBottomWidth borderCollapse borderColor borderImage borderImageOutset borderImageRepeat borderImageSlice borderImageSource borderIm
🌐
DEV Community
dev.to › mlgvla › javascript-using-the-map-object-322e
JavaScript: Using the Map Object - DEV Community
March 1, 2022 - The Map object in JavaScript bears a strong resemblance to the standard Object that uses key/value...
🌐
Reddit
reddit.com › r/javascript › why you should prefer map over object in javascript
r/javascript on Reddit: Why You Should Prefer Map Over Object In JavaScript
June 20, 2022 - Sure you can iterate over an map. You can iterate over every Object with Object.entries(obj). That doesn't necessarily say that it's optimised for that use. ... Initializing with values is awkward. You pass it an array of arrays, where the inner arrays must be length 2. Ideally, this would be a tuple but JavaScript lacks those.
🌐
Telerik
telerik.com › blogs › javascript-map-object
The JavaScript Map Object
January 18, 2024 - In the above code, we create a Map object map with two key-value pairs. We then access the size property to retrieve the number of pairs in the Map. While standard JavaScript objects (e.g., {}) are commonly used for key-value storage, Map objects offer several advantages that can make them a better choice in certain scenarios: