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 › Object › entries
Object.entries() - JavaScript - MDN Web Docs
// Strings have indices as enumerable own properties console.log(Object.entries("foo")); // [ ['0', 'f'], ['1', 'o'], ['2', 'o'] ] // Other primitives except undefined and null have no own properties console.log(Object.entries(100)); // [] The Map() constructor accepts an iterable of entries.
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. :-)

🌐
Medium
medium.com › @conboys111 › how-do-object-keys-object-values-and-object-entries-differ-in-javascript-8d1c19901ecb
How Do Object.keys(), Object.values(), and Object.entries() Differ in JavaScript? | by myHotTake | Medium
September 27, 2024 - Finally, I grabbed my Object.entries() map. This one was more detailed, like a catalog that showed both the names and values together, side by side. This map forced me to look at every treasure as a pair: the tag and the treasure itself. It felt like I was flipping through a scrapbook where ...
🌐
W3Schools
w3schools.com › jsref › jsref_object_entries.asp
JavaScript Object.entries() Method
Object.entries() returns the keys and values of any object types. The methods above return an Iterable (enumerable array). Iterables makes it simpler to use objects in loops and to convert objects into maps.
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › data types
Object.keys, values, entries
Use Object.entries(obj) to get an array of key/value pairs from obj. Use array methods on that array, e.g. map, to transform these key/value pairs.
🌐
Sentry
sentry.io › sentry answers › javascript › map function for objects (instead of arrays)
Map function for objects (instead of arrays) - JavaScript
Alternatively, you can use the Object.entries() method to convert the obj argument to a nested array where the first element of each nested array is an object key and the second element of each nested array is an object value.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-object-entries-method
JavaScript Object entries() Method - GeeksforGeeks
Map · Math · Number · Boolean · Exercise · Last Updated : 18 Sep, 2024 · The Object.entries() method in JavaScript is used to retrieve an array of an object's enumerable property [key, value] pairs.
Published   September 18, 2024
Find elsewhere
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Object › fromEntries
Object.fromEntries() - JavaScript - MDN Web Docs
The Object.fromEntries() static method transforms a list of key-value pairs into an object. const entries = new Map([ ["foo", "bar"], ["baz", 42], ]); const obj = Object.fromEntries(entries); console.log(obj); // Expected output: Object { foo: "bar", baz: 42 }
🌐
DEV Community
dev.to › attacomsian › object-entries-and-object-values-methods-in-javascript-3l8c
Object.entries Javascript: Object.entries() and Object.values() Methods in JavaScript - DEV Community
February 17, 2020 - const entries = new Map([ ['foo', 'bar'], ['baz', 42] ]); const obj = Object.fromEntries(entries); console.log(obj); // expected output: Object { foo: "bar", baz: 42 }
🌐
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 - An array has also the fix insertion order. But instead of Map an Array is hardly optimised for iteration while map is optimised for pointer access. Sure you can iterate over an map. You can iterate over every Object with Object.entries(obj).
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Map › entries
Map.prototype.entries() - JavaScript - MDN Web Docs
July 20, 2025 - const myMap = new Map(); myMap.set("0", "foo"); myMap.set(1, "bar"); myMap.set({}, "baz"); const mapIter = myMap.entries(); console.log(mapIter.next().value); // ["0", "foo"] console.log(mapIter.next().value); // [1, "bar"] console.log(mapIter.next().value); // [Object, "baz"]
🌐
Vultr Docs
docs.vultr.com › javascript › standard-library › Object › entries
JavaScript Object entries() - Get Object Entries | Vultr Docs
September 27, 2024 - The Object.entries() method in JavaScript is a useful tool for accessing a list of a given object's own enumerable string-keyed property [key, value] pairs in the form of an array.
🌐
GitHub
github.com › mmalecki › object-entries-map
GitHub - mmalecki/object-entries-map · GitHub
const objectEntriesMap = require('object-entries-map') const obj = { foo: 'bar' } objectEntriesMap(obj, ([key, value]) => [value, key]) // => { bar: 'foo' }
Author   mmalecki
🌐
Esdiscuss
esdiscuss.org › topic › object-entries-in-2015
Object.entries in 2015
A hypothetical Map.from > would merely be the same as the Map constructor! Not quite true: Array.from, unlike every other iterable consumer in the spec, also takes array-like plain objects: `Array.from({0:'E', 1:'S', 2:'6', length:3})` produces `["E","S","6"]`. The motivation for Map.from is that you could feed it a "map-like object" (that is, an object with no iterator) and get a Map in return: `Map.from({ foo: 1, bar: 2 })` would behave as `new Map([['foo',1],['bar',2]])`.
🌐
Attacomsian
attacomsian.com › blog › object-entries-values-javascript
Object.entries() and Object.values() methods in JavaScript
September 19, 2022 - // using `for...of` loop for (const ... takes an iterable to initialize a map object, the Object.entries() method can be used to create a map from an object: const map = new Map(Object.entries(birds)) console.log(map.size) ...
🌐
SamanthaMing
samanthaming.com › tidbits › 90-object-from-entries
JavaScript Object.fromEntries() | SamanthaMing.com
We got Object.entries() which converts an object → array. But what if you want to do the reverse? Wonder no more! Use Object.fromEntries() to array → object 👏 · const keyValuePair = [ ['cow', '🐮'], ['pig', '🐷'], ]; Object.fromEntries(keyValuePair); // { cow: '🐮', pig: '🐷' } Object.fromEntries 101 · Array → Object with Object.fromEntries · Map → Object with Object.fromEntries ·
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › data types
Map and Set
If we have a plain object, and we’d like to create a Map from it, then we can use built-in method Object.entries(obj) that returns an array of key/value pairs for an object exactly in that format.
🌐
Zipy
zipy.ai › blog › map-function-for-objects-instead-of-arrays
map function for objects instead of arrays
April 12, 2024 - The Object.entries method returns ... leverage this method along with the map function to iterate over each key-value pair of an object and apply a transformation function....
🌐
30 Seconds of Code
30secondsofcode.org › home › javascript › object › map to object
How can I convert a Map to a JavaScript object and vice versa? - 30 seconds of code
February 11, 2024 - const objectToMap = obj => new Map(Object.entries(obj)); objectToMap({a: 1, b: 2}); // Map {'a' => 1, 'b' => 2}
Top answer
1 of 2
5

If the data is coming from an external source, then your code can't know it's type for sure. This is why JSON.parse(myJson) returns the type any.

But if you are reasonably sure what for the server returns you can write your own type and cast your data to that type.

The type of each item in that response looks something like:

interface MyItem {
  amount: number
  id: number
  image: { url: string }
  name: string
  price: number
}

And then the api returns those in an object with string keys:

type MyApiResponse = { [key: string]: MyItem }

And lastly, you just need to tell typescript that you, the programmer, know what type this data really is with a cast:

const transformedData = Object.entries(data as MyApiResponse).map(([key, value]) => {
  return {
    ...value,
    id: key,
  };
});

const test = transformedData[0].image.url // string

Playground

2 of 2
0

interface Data {
    amount: number,
    id: number,
    image: {
        url: string
    },
    name: string,
    price: number
}

interface DataMap {
    [key:string]: Data
}

const data:DataMap =  {
        "-MnI78G4UBVNSdurDCQK": {
            "amount": 1,
            "id": 1,
            "image": {
                "url": "https://store.storeimages.cdn-apple.com/4668/as-images.apple.com/is/iphone-13-pro-family-hero?wid=940&hei=1112&fmt=png-alpha&.v=1631220221000"
            },
            "name": "iPhone 13 Pro",
            "price": 5299
        },
      

  "-MnI78IKVgJnmF_8XTZ6": {
        "amount": 2,
        "id": 2,
        "image": {
            "url": "https://cdn.x-kom.pl/i/setup/images/prod/big/product-new-big,,2019/7/pr_2019_7_17_13_43_4_952_00.jpg"
        },
        "name": "Macbook Air M1 16gb 512gb",
        "price": 7999
    }
}

You can use given interfaces to describe your data structure.