diff --git a/README.md b/README.md index c2fb8b122..9b5bca310 100644 --- a/README.md +++ b/README.md @@ -198,6 +198,7 @@ View contents * [`cleanObj`](#cleanobj) +* [`lowercaseKeys`](#lowercasekeys) * [`objectFromPairs`](#objectfrompairs) * [`objectToPairs`](#objecttopairs) * [`orderBy`](#orderby) @@ -3020,6 +3021,34 @@ cleanObj(testObj, ['a'], 'children'); // { a: 1, children : { a: 1}}
[⬆ Back to top](#table-of-contents) +### lowercaseKeys + +Creates a new object from the specified object, where all the keys are in lowercase. + +Use `Object.keys()` and `Array.reduce()` to create a new object from the specified object. +Convert each key in the original object to lowercase, using `String.toLowerCase()`. + +```js +const lowercaseKeys = obj => + Object.keys(obj).reduce((acc, key) => { + acc[key.toLowerCase()] = obj[key]; + return acc; + }, {}); +``` + +
+Examples + +```js +const myObj = { Name: 'Adam', sUrnAME: 'Smith' }; +const myObjLower = lowercaseKeys(myObj); // {name: 'Adam', surname: 'Smith'}; +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### objectFromPairs Creates an object from the given key-value pairs. diff --git a/docs/index.html b/docs/index.html index 400939957..021fc8dd3 100644 --- a/docs/index.html +++ b/docs/index.html @@ -225,6 +225,7 @@

Object

cleanObj +lowercaseKeys objectFromPairs objectToPairs orderBy @@ -1410,6 +1411,19 @@ Also if you give it a special key (childIndicator) it will search d
const testObj = { a: 1, b: 2, children: { a: 1, b: 2 } };
 cleanObj(testObj, ['a'], 'children'); // { a: 1, children : { a: 1}}
 
+

lowercaseKeys

+

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

+

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

+
const lowercaseKeys = obj =>
+  Object.keys(obj).reduce((acc, key) => {
+    acc[key.toLowerCase()] = obj[key];
+    return acc;
+  }, {});
+
+
const myObj = { Name: 'Adam', sUrnAME: 'Smith' };
+const myObjLower = lowercaseKeys(myObj); // {name: 'Adam', surname: 'Smith'};
+

objectFromPairs

Creates an object from the given key-value pairs.

Use Array.reduce() to create and combine key-value pairs.

diff --git a/snippets/lowercaseKeys.md b/snippets/lowercaseKeys.md index 6d4f63edf..d86f4ec2b 100644 --- a/snippets/lowercaseKeys.md +++ b/snippets/lowercaseKeys.md @@ -7,10 +7,13 @@ Convert each key in the original object to lowercase, using `String.toLowerCase( ```js const lowercaseKeys = obj => - Object.keys(obj).reduce((acc,key) => {acc[key.toLowerCase()] = obj[key]; return acc;},{}); + Object.keys(obj).reduce((acc, key) => { + acc[key.toLowerCase()] = obj[key]; + return acc; + }, {}); ``` ```js -const myObj = {Name: 'Adam', sUrnAME: 'Smith'}; +const myObj = { Name: 'Adam', sUrnAME: 'Smith' }; const myObjLower = lowercaseKeys(myObj); // {name: 'Adam', surname: 'Smith'}; ```