How to Lowercase all Keys in an Object in JavaScript
When working with data from different sources, such as APIs or databases, you may receive objects with inconsistent key casing (e.g., NAME, Name, name). To standardize this data for easier processing, a common task is to convert all keys in an object to lowercase.
This guide will demonstrate the most modern and efficient way to accomplish this using a combination of Object.entries(), map(), and Object.fromEntries(). We will also cover a more traditional approach using reduce() for comparison.
The Core Method (Recommended): Object.fromEntries() and map()
This approach is the most modern, declarative, and often the most readable way to transform object keys. It involves converting the object to an array of [key, value] pairs, transforming the keys, and then converting the array back into an object.
For example, you have an object with inconsistent, uppercase keys.
// Problem: We need to convert all keys to lowercase.
const user = {
FIRST_NAME: 'Tom',
'LAST-NAME': 'Nolan',
Age: 25
};
This function concisely implements the three-step transformation as solution:
function lowercaseKeys(obj) {
const entries = Object.entries(obj);
const mappedEntries = entries.map(([key, value]) => {
return [key.toLowerCase(), value];
});
return Object.fromEntries(mappedEntries);
}
// Example Usage:
const user = { FIRST_NAME: 'Tom', 'LAST-NAME': 'Nolan', Age: 25 };
const lowercasedUser = lowercaseKeys(user);
console.log(lowercasedUser);
// Output: { first_name: 'Tom', 'last-name': 'Nolan', age: 25 }
You can also write this as a more compact one-liner:
const lowercasedUser = Object.fromEntries(
Object.entries(user).map(([key, value]) => [key.toLowerCase(), value])
);
An Alternative Method: Using Object.keys() and reduce()
This is another powerful, functional approach using the Array.prototype.reduce() method. It iterates over the object's keys and builds a new object from scratch.
Solution:
function lowercaseKeys(obj) {
return Object.keys(obj).reduce((accumulator, key) => {
accumulator[key.toLowerCase()] = obj[key];
return accumulator;
}, {});
}
// Example Usage:
const user = { FIRST_NAME: 'Tom', 'LAST-NAME': 'Nolan', Age: 25 };
const lowercasedUser = lowercaseKeys(user);
console.log(lowercasedUser);
// Output: { first_name: 'Tom', 'last-name': 'Nolan', age: 25 }
This method is also very effective and was more common before Object.fromEntries() became widely available.
How the Core Method Works
Let's break down the Object.fromEntries() and map() approach step-by-step.
Step 1: Convert the Object to an Array of Entries
Object.entries(obj) transforms the object into an array where each element is another array containing a [key, value] pair.
const user = { FIRST_NAME: 'Tom', Age: 30 };
const entries = Object.entries(user);
console.log(entries);
// Output: [ ['FIRST_NAME', 'Tom'], ['Age', 30] ]
Step 2: Map Over the Entries to Transform the Keys
We use Array.prototype.map() to create a new array based on the entries array. For each [key, value] pair, we return a new pair where the key has been converted to lowercase. We use destructuring assignment ([key, value]) to make the code cleaner.
const mappedEntries = entries.map(([key, value]) => {
return [key.toLowerCase(), value];
});
console.log(mappedEntries);
// Output: [ ['first_name', 'Tom'], ['age', 30] ]
Step 3: Convert the Array of Entries Back to an Object
Object.fromEntries() performs the reverse of Object.entries(). It takes an array of [key, value] pairs and transforms it back into a new object.
const finalObject = Object.fromEntries(mappedEntries);
console.log(finalObject);
// Output: { first_name: 'Tom', age: 30 }
Conclusion
Standardizing the keys of an object is a common data transformation task. Modern JavaScript provides powerful, functional methods to do this cleanly and efficiently.
- The combination of
Object.entries(),map(), andObject.fromEntries()is the recommended best practice. It is declarative, easy to read, and leverages modern JavaScript features. - The
reduce()method is a perfectly valid and powerful alternative, which builds the new object iteratively.
Both methods are vastly superior to a traditional for...in loop as they avoid mutating the original object and are expressed in a cleaner, functional style.