You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
21 lines
502 B
21 lines
502 B
const deepMap = (obj, fn) => {
|
|
const deepMapper = val => (isObject(val)) ? deepMap(val, fn) : fn(val);
|
|
if (Array.isArray(obj)) {
|
|
return obj.map(deepMapper);
|
|
}
|
|
if (isObject(obj)) {
|
|
return mapObject(obj, deepMapper);
|
|
}
|
|
return obj;
|
|
};
|
|
|
|
const mapObject = (obj, fn) => Object.keys(obj).reduce(
|
|
(res, key) => {
|
|
res[key] = fn(obj[key]);
|
|
return res;
|
|
}, {});
|
|
|
|
const isObject = myVar => myVar && typeof myVar === 'object';
|
|
|
|
export default deepMap;
|