0
我有以下遞歸函數,但它不起作用... 它搜索到「firstPageChild」,然後完成。任何人都可以發現這裏有什麼問題?.. 我敢肯定它的東西簡單,但其目前絆倒我..javascript遞歸函數不能正常工作
var pageMap = [{"pageID" : "mainPage",
"children": [{"pageID" : "firstPage",
"children": [{"pageID" : "firstPageChild",
"children": []
}]
},
{"pageID" : "secondPage",
"children": [{"pageID" : "secondPageChild1",
"children": []
}, {"pageID" : "secondPageChild2",
"children": []
}]
},
{"pageID" : "thirdPage",
"children": [{"pageID" : "thirdPageChild1",
"children": []
}, {"pageID" : "thirdPageChild2",
"children": []
}]
}]
}];
function findObjectById(root, id) {
debugger;
var k, pageVar;
if (root.children) {
for (k in root.children) {
pageVar = root.children[k];
if (pageVar.pageID == id) {
return pageVar;
}
else if (pageVar.children.length) {
return findObjectById(pageVar, id);
}
}
}
};
for (var i = 0, len = pageMap.length; i < len; i++) {
var myObj = findObjectById(pageMap[i], "secondPageChild2");
}
console.log(myObj);
謝謝...感謝幫助 – blu10