map over nested object javascript code example
Example: javascript map value nested object
function deepMap(obj, mapfn) {
function recurse(obj) {
let res = {}
console.log(JSON.stringify(obj))
for (const key in obj) {
const value = obj[key];
if (value && typeof value === 'object') {
res[key] = recurse(value);
} else {
res[key] = mapfn(value);
}
}
return res
}
return recurse(obj);
}
deepMap({'a':1, 'b':{'c':3}}, (val) => val+1) //{'a':2, 'b':{'c':4}}