JavaScript developers often work with both Map objects and plain objects. A common requirement is filtering entries in a Map and then converting the result into a standard JavaScript object. What’s the best way to do this efficiently and cleanly?
In this article, we’ll explore the recommended approach and discuss why it works well in modern JavaScript.
Understanding the Problem
Suppose you have a Map containing user data:
const users = new Map([
['john', 25],
['alice', 32],
['bob', 19]
]);
You want to keep only users who are at least 21 years old and convert the result into a plain object.
Expected output:
{
john: 25,
alice: 32
}
Recommended Solution
The most concise and readable solution is to:
- Convert the Map into an array using
Array.from(). - Filter the entries.
- Convert the filtered entries back into an object using
Object.fromEntries().
const result = Object.fromEntries(
Array.from(users).filter(([name, age]) => age >= 21)
);
console.log(result);
Output:
{
john: 25,
alice: 32
}
Why This Approach Is Preferred
Readability
The code clearly communicates each step:
- Convert the Map to an array.
- Filter the entries.
- Build an object.
Developers can quickly understand what’s happening without additional comments.
Modern JavaScript Support
Object.fromEntries() was introduced in ES2019 and is supported by all modern browsers and Node.js versions.
Functional Style
Using filter() avoids manual loops and temporary variables, resulting in cleaner code.
Alternative Using a Loop
You can also achieve the same result with a traditional loop:
const result = {};
for (const [name, age] of users) {
if (age >= 21) {
result[name] = age;
}
}
While this works perfectly, it is more verbose than the functional approach.
Performance Considerations
For small and medium-sized datasets, the difference is negligible. For very large Maps containing thousands of entries, a manual loop may offer slightly better performance because it avoids creating intermediate arrays.
However, in most real-world applications, readability and maintainability are more important than micro-optimizations.
Creating a Reusable Utility Function
If you perform this operation frequently, consider creating a helper function:
function filterMapToObject(map, predicate) {
return Object.fromEntries(
Array.from(map).filter(([key, value]) => predicate(key, value))
);
}
Usage:
const adults = filterMapToObject(
users,
(name, age) => age >= 21
);
console.log(adults);
Infographic

Conclusion
The best approach to filter a Map and convert it into a JavaScript object is to combine Array.from(), filter(), and Object.fromEntries(). This method is concise, readable, and well-supported in modern JavaScript environments.
For performance-critical scenarios involving extremely large datasets, a traditional loop may be marginally faster. For most applications, however, the functional approach offers the best balance between clarity and maintainability.