Last updated: Mar 4, 2024
Reading timeยท3 min
To lowercase all keys in an object:
Object.keys()
method to get an array of the object's keys.Array.reduce()
method to iterate over the array.function lowercaseKeys(obj) { return Object.keys(obj).reduce((accumulator, key) => { accumulator[key.toLowerCase()] = obj[key]; return accumulator; }, {}); } const obj = { NAME: 'bobby hadz', AGE: 30, COUNTY: 'Chile', }; // ๐๏ธ { name: 'bobby hadz', age: 30, county: 'Chile' } console.log(lowercaseKeys(obj));
We created a reusable function that converts an object's keys to lowercase.
The first step is to use the Object.keys() method to get an array of the object's keys.
const obj = { NAME: 'bobby hadz', AGE: 30, COUNTY: 'Chile', }; // ๐๏ธ [ 'NAME', 'AGE', 'COUNTY' ] console.log(Object.keys(obj));
We used the Array.reduce() method to iterate over the array of keys.
The second argument we passed to reduce()
method is the initial value of the
accumulator
variable.
We initialized the variable to an empty object.
function lowercaseKeys(obj) { return Object.keys(obj).reduce((accumulator, key) => { accumulator[key.toLowerCase()] = obj[key]; return accumulator; }, {}); }
We convert each key to lowercase using the toLowerCase() method and assign the key-value pair to the accumulated object.
The Array.reduce()
method will return an object where all keys are converted
to lowercase.
An alternative approach is to use the Object.entries and Object.fromEntries methods.
Array.forEach()
This is a three-step process:
Array.forEach()
method to iterate over the keys of the original
object.function lowercaseKeys(obj) { const newObject = {}; Object.keys(obj).forEach(key => { newObject[key.toLowerCase()] = obj[key]; }); return newObject; } const obj = { NAME: 'bobby hadz', AGE: 30, COUNTY: 'Chile', }; const result = lowercaseKeys(obj); // ๐๏ธ { name: 'bobby hadz', age: 30, county: 'Chile' } console.log(result);
We used the Object.keys()
method to get an array of the object's keys and used
the Array.forEach()
method to iterate over the array.
The function we passed to the Array.forEach() method gets called with each element in the array.
On each iteration, we convert the current key to lowercase and assign the key-value pair to a new object.
The last step is to return the new object from the function.
Object.fromEntries()
This is a three-step process:
Object.entries()
method to get an array of key-value pairs.map()
method to lowercase all keys in the array.Object.fromEntries()
method on the result.function lowercaseKeys(obj) { const entries = Object.entries(obj); return Object.fromEntries( entries.map(([key, value]) => { return [key.toLowerCase(), value]; }), ); } const obj = { NAME: 'bobby hadz', AGE: 30, COUNTY: 'Chile', }; // ๐๏ธ { name: 'bobby hadz', age: 30, county: 'Chile' } console.log(lowercaseKeys(obj));
The Object.entries() method returns an array of the given object's key-value pairs.
const obj = { NAME: 'bobby hadz', AGE: 30, COUNTY: 'Chile', }; // ๐๏ธ [ [ 'NAME', 'bobby hadz' ], [ 'AGE', 30 ], [ 'COUNTY', 'Chile' ] ] console.log(Object.entries(obj));
The function we passed to the Array.map() method gets called with each element in the array.
function lowercaseKeys(obj) { const entries = Object.entries(obj); return Object.fromEntries( entries.map(([key, value]) => { return [key.toLowerCase(), value]; }), ); }
We used destructuring assignment to assign the current key and value to variables.
const [key, value] = ['NAME', 'bobby hadz']; console.log(key); // ๐๏ธ "NAME" console.log(value); // ๐๏ธ "bobby hadz"
On each iteration, we return a new array with the key converted to lowercase.
This gives us an array of key-value pairs where all keys are lowercase.
// ๐๏ธ [ [ 'name', 'bobby hadz' ], [ 'age', 30 ] ] console.log( [ ['NAME', 'bobby hadz'], ['AGE', 30], ].map(([key, value]) => { return [key.toLowerCase(), value]; }), );
The last step is to pass the array of key-value pairs to the
Object.fromEntries
method to convert it back to an object.
// ๐๏ธ { name: 'bobby hadz', age: 30 } console.log( Object.fromEntries([ ['name', 'bobby hadz'], ['age', 30], ]), );
The Object.fromEntries() transforms a list of key-value pairs into an object.
This was a three-step process:
You can learn more about the related topics by checking out the following tutorials: