Add lowercaseKeys

This commit is contained in:
Angelos Chalaris
2017-12-29 13:28:18 +02:00
parent 12d83c89bf
commit 2512cb6d34
2 changed files with 19 additions and 0 deletions

18
snippets/lowercaseKeys.md Normal file
View File

@ -0,0 +1,18 @@
### 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;},{});
```
```js
let myObj = {Name: 'Adam', sUrnAME: 'Smith'};
let myObjLower = lowercaseKeys(myObj);
console.log(myObj); // {Name: 'Adam', sUrnAME: 'Smith'};
console.log(myObjLower); // {name: 'Adam', surname: 'Smith'};
```