Question
How to Map Over Object Properties in JavaScript
Question
I have a JavaScript object:
const myObject = { a: 1, b: 2, c: 3 };
I want a native method similar to Array.prototype.map() that I could use like this:
const newObject = myObject.map(function (value, key) {
return value * value;
});
// newObject should become: { a: 1, b: 4, c: 9 }
Does JavaScript provide a built-in map() function for plain objects? I am using Node.js, so browser compatibility is not a concern.
Short Answer
By the end of this page, you will understand why plain JavaScript objects do not have a built-in map() method, and how developers typically achieve the same result using Object.keys(), Object.entries(), Object.fromEntries(), loops, or small helper functions. You will also learn when to use an object versus an array for transformations.
Concept
Plain JavaScript objects and arrays are different data structures.
- Arrays are ordered collections and come with array methods like
map(),filter(), andreduce(). - Objects are key-value collections and do not have a built-in
map()method.
So the direct answer is: No, JavaScript does not provide a native map() method for plain objects.
However, you can still "map over" an object's properties by converting the object into something iterable, transforming it, and then turning it back into an object.
A common modern pattern is:
- Get the object's entries with
Object.entries(obj) - Use array methods like
map() - Rebuild an object with
Object.fromEntries()
Example:
const myObject = { a: 1, b: 2, c: 3 };
const newObject = Object.fromEntries(
Object.(myObject).( [key, value * value])
);
.(newObject);
Mental Model
Think of an array as a row of numbered boxes:
- box 0
- box 1
- box 2
Because arrays are ordered, JavaScript gives them tools like map() to process each box in sequence.
Think of an object as a set of labeled drawers:
- drawer
a - drawer
b - drawer
c
Objects are organized by keys, not by numeric positions. Since they are not arrays, they do not automatically get array methods like map().
So when you want to "map an object," you usually:
- open all the labeled drawers (
Object.entries()) - transform each drawer's contents (
map()) - rebuild the cabinet (
Object.fromEntries())
That is the core idea.
Syntax and Examples
The most common ways to transform object values are shown below.
1. Using Object.entries() and Object.fromEntries()
const myObject = { a: 1, b: 2, c: 3 };
const squared = Object.fromEntries(
Object.entries(myObject).map(([key, value]) => [key, value * value])
);
console.log(squared); // { a: 1, b: 4, c: 9 }
How it works
Object.entries(myObject)returns:
[['a', 1], ['b', 2], ['c', 3]]
- Then
.map()transforms each[key, value] Object.fromEntries()builds a new object from the transformed pairs
Step by Step Execution
Consider this example:
const myObject = { a: 1, b: 2, c: 3 };
const result = Object.fromEntries(
Object.entries(myObject).map(([key, value]) => [key, value * 2])
);
console.log(result);
Here is what happens step by step.
Step 1: Start with the original object
{ a: 1, b: 2, c: 3 }
Step 2: Convert the object into entries
Object.entries(myObject)
Result:
[['a', 1], ['b', 2], [, ]]
Real World Use Cases
Object mapping is useful whenever you need to transform values while keeping the same keys.
Common examples
1. Formatting API response data
const prices = { apple: 1.2, banana: 0.8 };
const formatted = Object.fromEntries(
Object.entries(prices).map(([key, value]) => [key, `$${value.toFixed(2)}`])
);
console.log(formatted); // { apple: '$1.20', banana: '$0.80' }
2. Converting configuration values
const envConfig = { port: '3000', debug: 'true' };
const parsed = {
port: Number(envConfig.port),
debug: envConfig.debug === 'true'
};
Or with a helper when rules are consistent.
3. Normalizing imported data
Real Codebase Usage
In real projects, developers usually do not try to add map() directly to all objects. Instead, they use one of these patterns.
1. Small utility function
A reusable helper keeps code clean.
function mapObjectValues(obj, transform) {
return Object.fromEntries(
Object.entries(obj).map(([key, value]) => [key, transform(value, key)])
);
}
This is common in service layers, data formatting utilities, and validation modules.
2. Guard clauses before transformation
Real code often validates input first.
function mapObjectValues(obj, transform) {
if (obj === null || typeof obj !== 'object' || Array.isArray(obj)) {
throw new TypeError('Expected a plain object');
}
return Object.(
.(obj).( [key, (value, key)])
);
}
Common Mistakes
1. Trying to call .map() directly on an object
This does not work:
const obj = { a: 1, b: 2 };
obj.map((value) => value * 2);
Why it fails:
- plain objects do not have a
map()method
Use this instead:
const result = Object.fromEntries(
Object.entries(obj).map(([key, value]) => [key, value * 2])
);
2. Using Object.keys() but forgetting to build a new object
Broken idea:
const obj = { a: 1, b: 2 };
const result = Object.(obj).( obj[key] * );
.(result);
Comparisons
| Approach | Works on | Returns | Best for | Example |
|---|---|---|---|---|
Array.prototype.map() | Arrays | New array | Ordered lists | [1, 2, 3].map(x => x * 2) |
Object.keys() + loop | Objects | Whatever you build | Simple object transformations | build result manually |
Object.entries() + map() + Object.fromEntries() | Objects | New object | Clean value/key transformation | transform key-value pairs |
Cheat Sheet
// No native object.map() exists for plain objects
Common pattern
const result = Object.fromEntries(
Object.entries(obj).map(([key, value]) => [key, transform(value, key)])
);
Reusable helper
function mapObject(obj, fn) {
return Object.fromEntries(
Object.entries(obj).map(([key, value]) => [key, fn(value, key)])
);
}
Alternatives
Using Object.keys()
const result = {};
Object.keys(obj).forEach(() => {
result[key] = (obj[key], key);
});
FAQ
Is there a native map() for JavaScript objects?
No. Plain objects do not have a built-in map() method.
What is the modern way to map over an object in JavaScript?
The most common modern approach is:
Object.fromEntries(
Object.entries(obj).map(([key, value]) => [key, transform(value, key)])
)
Can I use Object.keys() instead of Object.entries()?
Yes. Object.keys() works well if you only need the keys and can access values with obj[key].
Why does Object.keys(obj).map(...) return an array?
Because Object.keys(obj) returns an array, and array map() always returns a new array.
Should I add map() to Object.prototype?
Mini Project
Description
Build a small utility that transforms the values of a settings object while keeping the same keys. This demonstrates the standard JavaScript pattern for object mapping and mirrors how developers normalize configuration or API data in real projects.
Goal
Create a reusable mapObjectValues() function and use it to transform an object's values into a new object.
Requirements
- Create a function named
mapObjectValuesthat accepts an object and a callback. - Return a new object instead of mutating the original one.
- Pass both
valueandkeyto the callback. - Use the function to transform a sample object of numeric values.
- Print both the original object and the transformed object.
Keep learning
Related questions
Adding Table Rows with jQuery: append(), Limits, and Best Practices
Learn how to add table rows in jQuery using append(), what elements are allowed in tables, and safer ways to build rows dynamically.
Bower vs npm: What’s the Difference in JavaScript Package Management?
Learn the plain difference between Bower and npm, how each manages packages, and why npm replaced Bower in most JavaScript projects.
Can `(a == 1 && a == 2 && a == 3)` Ever Be True in JavaScript?
Learn how JavaScript type coercion, loose equality, and custom object conversion can make `a == 1 && a == 2 && a == 3` true.