How to Lowercase all Keys in an Object using JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 4, 2024
3 min

banner

# Lowercase all Keys in an Object in JavaScript

To lowercase all keys in an object:

  1. Use the Object.keys() method to get an array of the object's keys.
  2. Use the Array.reduce() method to iterate over the array.
  3. Convert each key to lowercase and assign the key-value pairs to a new object.
index.js
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));

lowercase all object keys

The code for this article is available on GitHub

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.

index.js
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.

index.js
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.

# Lowercase all Keys in an Object using Array.forEach()

This is a three-step process:

  1. Define a new variable and initialize it to an empty object.
  2. Use the Array.forEach() method to iterate over the keys of the original object.
  3. Lowercase each key and assign each key-value pair to the new object.
index.js
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);

lowercase all keys in object using foreach

The code for this article is available on GitHub

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.

# Lowercase all Keys in an Object using Object.fromEntries()

This is a three-step process:

  1. Use the Object.entries() method to get an array of key-value pairs.
  2. Use the map() method to lowercase all keys in the array.
  3. Call the Object.fromEntries() method on the result.
index.js
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));

lowercase all keys in object using fromentries

The code for this article is available on GitHub

The Object.entries() method returns an array of the given object's key-value pairs.

index.js
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.

index.js
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.

index.js
const [key, value] = ['NAME', 'bobby hadz']; console.log(key); // ๐Ÿ‘‰๏ธ "NAME" console.log(value); // ๐Ÿ‘‰๏ธ "bobby hadz"
The code for this article is available on GitHub

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.

index.js
// ๐Ÿ‘‡๏ธ [ [ '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.

index.js
// ๐Ÿ‘‡๏ธ { 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:

  1. Get an array of key-value pairs.
  2. Convert each key to lowercase.
  3. Create an object using the key-value pair array.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev