Skip to content

Instantly share code, notes, and snippets.

@elclanrs
Created August 15, 2014 21:25
Show Gist options
  • Save elclanrs/44316e72dfa10486dce3 to your computer and use it in GitHub Desktop.
Save elclanrs/44316e72dfa10486dce3 to your computer and use it in GitHub Desktop.
var deepObjMap = function(f, obj) {
return Object.keys(obj).reduce(function(acc, k) {
if ({}.toString.call(obj[k]) == '[object Object]') {
acc[k] = deepMap(f, obj[k])
} else {
acc[k] = f(obj[k], k)
}
return acc
},{})
}
// Example
var add1 = function(x){return x + 1}
var o = {
a: 1,
b: {
c: 2,
d: {
e: 3
}
}
}
console.log(deepObjMap(add1, o))
//^ { a: 2, b: { c: 3, d: { e: 4 } } }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment