我試圖獲取特定人員的後代列表。以下是我到目前爲止:試圖從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