Skip to main content

lowercaseKeys

TS JS Deno

Creates a new object from the specified object, where all the keys are in lowercase.

Use Object.keys() and Array.prototype.reduce() to create a new object from the specified object. Convert each key in the original object to lowercase, using String.toLowerCase().

typescript
type AnyObject = { [key: string]: any };
const lowercaseKeys = (obj: AnyObject, deep = false) =>
Object.keys(obj).reduce((acc, key) => {
acc[key.toLowerCase()] =
deep && typeof obj[key] === "object" ? lowercaseKeys(obj[key]) : obj[key];
return acc;
}, {} as AnyObject);
typescript
lowercaseKeys({ Name: "Adam", sUrnAME: "Smith" }); // {name: 'Adam', surname: 'Smith'};
lowercaseKeys({ Name: "Adam", adDress: { Pin: 700000 } }, true); // {name: 'Adam', address: {pin: 700000} }