Why can't I always get the value of d? I want to get the value of the specified node in a deeper object, which may be a string, a number, or a complex object.
var a = {
a: 1,
b: {
c: {
f: 3
},
d: 2
}
}
function fun(foo, key) {
for (var f in foo) {
if (f == key) {
return foo[f];
} else {
fun(foo[f], key)
}
}
}
console.log(fun(a, 'd'));