Revert "adds deepMapKeys snippet"

This reverts commit 73470c50d9.
This commit is contained in:
Ilia Aphtsiauri
2018-12-09 18:48:59 +02:00
parent 56e7689a97
commit cc652ac653
7 changed files with 2592 additions and 2647 deletions

View File

@ -1,59 +0,0 @@
### deepMapKeys
Deep maps an object keys.
Creates an object with the same values as the provided object and keys generated by running the provided function for each key.
Use Object.keys(obj) to iterate over the object's keys. Use Array.prototype.reduce() to create a new object with the same values and mapped keys using fn.
```js
const deepMapKeys = (obj, f) => (
Array.isArray(obj)
? obj.map(val => deepMapKeys(val, f))
: (typeof obj === 'object')
? Object.keys(obj).reduce((acc, current) => {
const val = obj[current];
acc[f(current)] = (val !== null && typeof val === 'object')
? deepMapKeys(val, f)
: acc[f(current)] = val;
return acc;
}, {})
: obj
);
```
```js
'use strict';
const obj = {
foo:'1',
isnull:null,
nested:{
bar:'1',
child:{
withArray:[
{
grandChild:[ 'hello' ]
}
]
}
}
}
const upperKeysObj = deepMapKeys(obj, (key) => key.toUpperCase());
/*
Formatted JSON Data
{
"FOO":"1",
"ISNULL":null,
"NESTED":{
"BAR":"1",
"CHILD":{
"WITHARRAY":[
{
"GRANDCHILD":[ 'hello' ]
}
]
}
}
}
*/
```