我试图获取特定人员的后代列表。以下是我到目前为止:试图从json树状结构递归创建结果数组JavaScript
function getDescendants(id, descendants){
children = getChildren(id);
if(children){
for (var child in children) {
if(children.hasOwnProperty(child)){
descendants.push(getDescendants(children[child].id, descendants));
}
}
}
return getPersonById(id);
}
此工作,直到它返回到初始调用,并已忘记了子数组。
的getChildren回报和儿童的对象数组 getPersonById返回
任何帮助/建议表示赞赏一个人对象
代码是不符合逻辑,为什么不回到'descendants'直接 –
@AbdelrhmanMohamed好的想象的后代甚至没有传递想象它的全球...如果不是我们做的(儿童儿童) if(children.hasOwnProperty(child)){ descendants.push(children [child]); getDescendants(children [child] .id); } \t \t}' – HobbitMafia
让我直说,你需要记住第一次调用getChildren时返回的原始数组吗?你是否想要偶然制作一棵B树? – Ryan