JSON爬虫函数返回undefined

Cor*_*win 1 javascript

我有一个递归JSON爬虫函数,它查找指定的函数然后返回它.但是,它返回undefined,我不知道为什么.

这是脚本:

function get(what, where){
    where = typeof(where) != 'undefined' ? where : user.object;
    for(entry in where){
        if(typeof(where[entry]) =="string"){
            if (entry == what) {
                result = where[entry];
                console.log(result)
                return result;
            }
        }else if(typeof(where[entry]) =="object"){   
            get(what, where[entry]);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

console.log正确的回报,但return语句下面失败.

Nic*_*ver 6

这是因为你没有返回代码的递归分支,这个:

get(what, where[entry]);
Run Code Online (Sandbox Code Playgroud)

应该:

return get(what, where[entry]);
Run Code Online (Sandbox Code Playgroud)

所以在那个分支上,虽然你一直在执行,但你没有返回结果,所以你得到了默认的回报:undefined.