这是源数据。我想用“你好”的来源,找到“向上”,最后得到一个数组“[最大值,最小值]”(如多树,找到根)我如何使用递归函数来处理数组?
var obj = {
'hello': {
"up": "world",
"down": "ccc"
},
'world': {
"up": ["max","min"],
"down": "hello"
},
'max': {
"up": null,
"down": "world"
},
'min': {
"up": null,
"down": "world"
},
'ccc': {
"up": "hello",
"down": null
}
}
我使用递归函数,但下面的代码不起作用。它返回“未定义”。 (如果“up”不是数组,则该功能起作用)。
function findRoot(source,key){
var up = source[key]['up'];
if(up==null){
return key
}else{
if(Object.prototype.toString.call(up)=='[object Array]'){
up.forEach(function(d){
return findRoot(source,d);
})
}else{
return findRoot(source,up)
}
}
}
我该如何解决此问题?
您可以加入预期的输出? – cjds
该功能的目标不明确。 – plalx