Update deepMapKeys

Resolves #1139
This commit is contained in:
Angelos Chalaris
2020-04-20 11:05:56 +03:00
parent 2176ab6e9a
commit 977dc31cfb

View File

@ -10,14 +10,15 @@ 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`. Use `Array.prototype.reduce()` to create a new object with the same values and mapped keys using `fn`.
```js ```js
const deepMapKeys = (obj, f) => const deepMapKeys = (obj, fn) =>
Array.isArray(obj) Array.isArray(obj)
? obj.map(val => deepMapKeys(val, f)) ? obj.map(val => deepMapKeys(val, fn))
: typeof obj === 'object' : typeof obj === 'object'
? Object.keys(obj).reduce((acc, current) => { ? Object.keys(obj).reduce((acc, current) => {
const key = fn(current);
const val = obj[current]; const val = obj[current];
acc[f(current)] = acc[key] =
val !== null && typeof val === 'object' ? deepMapKeys(val, f) : (acc[f(current)] = val); val !== null && typeof val === 'object' ? deepMapKeys(val, fn) : val;
return acc; return acc;
}, {}) }, {})
: obj; : obj;