Object.entries()
ES2017+Returns an array of a given object's own enumerable property [key, value] pairs.
Syntax
Object.entries(obj)Parameters
obj Object The object whose enumerable own property [key, value] pairs are to be returned
Return Value
An array of [key, value] pairs
Examples
const obj = { a: 1, b: 2 };
console.log(Object.entries(obj)); 📌 When to Use
Use Object.entries() when you need both keys and values during iteration. Perfect for transforming objects, creating Maps from objects, destructuring in for...of loops, or when you need to filter/map based on both key and value conditions.
⚠️ Common Mistakes
Forgetting to destructure the [key, value] pair in callbacks: use ([key, value]) => ... not (entry) => entry[0], entry[1].
Not realizing that modifying the returned arrays does not affect the original object - entries are snapshots, not live references.
Using Object.entries() when only keys or values are needed - it creates unnecessary [key, value] arrays that waste memory.
✅ Best Practices
Use with for...of and destructuring for clean iteration: for (const [key, value] of Object.entries(obj)) { ... }
Combine with Object.fromEntries() for object transformations: Object.fromEntries(Object.entries(obj).filter(...))
Convert objects to Maps easily: new Map(Object.entries(obj)) preserves key-value relationships with Map benefits.
⚡ Performance Notes
Object.entries() is the slowest of the three (keys/values/entries) because it must create a new two-element array for each property. The memory overhead is O(2n) for the inner arrays. For performance-critical code iterating over large objects, consider using Object.keys() with direct property access, or use a traditional for...in loop with hasOwnProperty check.
🌍 Real World Example
Environment Variable Parser
Transform environment variables by filtering and reformatting configuration objects with Object.entries().
const config = {
DB_HOST: 'localhost',
DB_PORT: '5432',
DB_NAME: 'myapp',
API_KEY: 'secret123',
DEBUG: 'true'
};
// Filter only DB-related config
const dbConfig = Object.fromEntries(
Object.entries(config)
.filter(([key]) => key.startsWith('DB_'))
.map(([key, value]) => [key.replace('DB_', '').toLowerCase(), value])
);
console.log(dbConfig);
// { host: 'localhost', port: '5432', name: 'myapp' }
// Convert string booleans to actual booleans
const typedConfig = Object.fromEntries(
Object.entries(config).map(([key, value]) => [
key,
value === 'true' ? true : value === 'false' ? false : value
])
);
console.log(typedConfig.DEBUG);
// true (boolean)