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"
}
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?
Similar Posts
- Lodash - object keys conversion to lowercase
- Get all keys from an object using Javascript
- Convert all characters to lowercase using toLowerCase() method in javascript
- Get number of keys in an object Javascript
- Convert object {key: value} to Array [[key, value]] Javascript
- Get all values as array items from an Object in Javascript
- How to Check if Arrays in a Object Are All Empty