javascript

Javascript to convert all object keys to lowercase

If you have an object that contains multiple keys with some characters in the uppercase format then you can use the methods explained in this post to convert them to lowercase,

let person = {
    "Name": "Richard",
    "Company": "Pied Pieper",
    "Algo Name": "Compression"
};

let key, keys = Object.keys(person);
let total = keys.length;

let result = {}

while (total--) {
    key = keys[total];
    result[key.toLowerCase()] = person[key];
}

console.log(result);

Output

{
  "algo name": "Compression",
  "company": "Pied Pieper",
  "name": "Richard"
}

To check the live demo of the above code Click Here

Using Object.entries() and map function

If you are using EcmaScript10 then you can use Object.fromEntries(), Object.entries() and map() function to convert the object keys to lowercase format.

Code Example

let person = {
    "Name": "Richard",
    "Company": "Pied Pieper",
    "Algo Name": "Compression"
};

const result = Object.fromEntries(
    Object.entries(person).map(([key, val]) => [key.toLowerCase(), val])
);

console.log(result);

Output

{
  "name": "Richard",
  "company": "Pied Pieper",
  "algo name": "Compression"
}

Live Demo of the above code

let person = {
    "Name": "Richard",
    "Company": "Pied Pieper",
    "Algo Name": "Compression"
};

var result_obj = Object.keys(person)
    .reduce((result, key) => {
        result[key.toLowerCase()] = person[key];
        return result;
    }, {});

console.log(result_obj);
You can also use Object.keys() function and chaining reduce() function with it.

Was this helpful?