this is the source data. I want to use a "hello" in source ,find the "up", finally,to get an array "[max,min]" (like multiple tree, find the roots)
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
}
}
I use a recursion function, but the code below doesn't work. It return "undefined". (if the "up" is not an array, the function works.)
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)
}
}
}
How can i fix this code?

