Map the values of a JavaScript object

Transform object entries into a new object, preserve the intended key scope, and distinguish shallow copies from deep updates.

Best practices
Illustration of files, modules, and dependencies

Plain objects do not inherit an array-style .map() method. Convert their entries to an array, map the key-value pairs, and rebuild an object with Object.fromEntries().

Transform values

const source = { a: 1, b: 2, c: 3 };
const scaled = Object.fromEntries(
  Object.entries(source).map(([key, value]) => [key, value * 10])
);
// { a: 10, b: 20, c: 30 }

Object.entries() returns the object's own enumerable string-keyed properties. It excludes inherited, non-enumerable, and symbol-keyed properties. The result is a new plain object, not a copy of the original prototype or property descriptors.

If you only need the transformed values, return an array:

const values = Object.values(source).map(value => value * 10);
// [10, 20, 30]

Transform keys or reuse a helper

const upper = Object.fromEntries(
  Object.entries(source).map(([key, value]) => [key.toUpperCase(), value])
);
// { A: 1, B: 2, C: 3 }

const mapObject = (object, transform) => Object.fromEntries(
  Object.entries(object).map(([key, value]) => [key, transform(value, key)])
);
const updated = mapObject(source, value => value * 10);

If two transformed keys become identical, the later entry replaces the earlier value. Define a collision policy if that would lose information.

Common questions

Does this modify the original object?

The examples construct a new outer object. They do not deep-clone nested values, and your callback can still mutate an object it receives. For immutable state updates, copy each nested level you intend to change.

A for...in loop can update the original object, but it also visits enumerable inherited properties unless you filter them. Object.entries() makes the own-property scope explicit.

What runtimes support it?

Object.fromEntries() is an ES2019 feature, available in Node.js 12 and modern browsers. Older runtimes need a compatible implementation or a library helper. Do not assume Node.js 10 supports this API merely because it supports some other object utilities.

What about a Map?

A Map is iterable but does not have an array-style map method. Transform its entries and construct a new Map:

const scores = new Map([['a', 1], ['b', 2]]);
const scaledScores = new Map(
  [...scores].map(([key, value]) => [key, value * 10])
);

This preserves Map key identity rather than converting keys to object property names.

References: Object.entries and Object.fromEntries.

Get a tailored plan

Contact us

Join the community

Scan with WeChat
to join the community

Try Guance

Start online and pay only for what you use.

Get started

Choose a Guance plan

Code hosting