需要更多的帮助,我想我有递归工作正常,但我无法得到原始请求项目的属性设置为正确的值。用递归函数和返回值
这里的目标是找到嵌套项目(基于Dictionary“parent”属性的分类法)的最顶层父项(或“ancestor”,depth = 0),并分配最初请求的嵌套项目的“ancestor”属性因此。
例如,在
苹果
- 红
- - 帝国
- - - “苹果” 新鲜
“新鲜” 的始祖应该设置为
虽然我试图在“按需”和个人的基础上,我打开一个解决方案,标记所有与同一祖先相关的所有孩子一举或声明,因为那样会可能会更有效率。
REQUEST
for (var mc:Object in taxonomy) {
var term = taxonomy[mc];
term["ancestor"] = getAncestor(term);
trace("Setting " + term.name + "'s ancestor as [" + term.ancestor + "]");
... }
功能
function getAncestor(term:Object):String {
var ancestor = "default";
for(var obj:Object in taxonomy) {
if(term.parent == taxonomy[obj].tid) { // If next object is current object's parent
if(taxonomy[obj].depth == 0) { // And if object's parent is a root object
// Then object's parent is the ancestor
trace(term.name + "'s parent IS the root (" + taxonomy[obj].name + "). DONE."); // "term" here is NOT originally requested term
return(taxonomy[obj].name); // Return DIRECTLY to function call and assign originally requested term with this name.
break; // Get the hell out of here
}
else { // If object's parent is not a root object
trace(term.name + "'s parent (" + taxonomy[obj].name + ") is NOT a root. LOOPING.");
getAncestor(taxonomy[obj]); // Step function again with current object's parent object as current object
}
}
}
return(ancestor);
}
最后,这里是基于我的许多调试语句的跟踪输出的一个片段:
治疗的父(吃障碍)不是根。循环。
饮食失调的母公司(身心)不是根。循环。
心身的父(疾病/疾病)是不是根。循环。
疾病/疾病的父(个人)不是根。循环。
个人的父(健康)是不是根。循环。
健康的父母是根(人)。 DONE。
定型处理的祖先为[默认]
正如你所看到的,而递归确实找到根,最初请求的项目仍然得到默认值。我错过了什么?
这样做。这次我差点拿到了。感谢您的简单修复! – atwixtor 2010-11-05 19:14:54