0

我有一个递归循环,如果在 ArrayCollection 的某个嵌套级别找到它应该返回值。一旦找到返回值并由函数返回,但在下一次迭代中返回值变回 null。我错过了什么或做错了什么?

// calling function
...
foundedItem = this.recursiveFindFunction(valueList); 
...

private function recursiveFindFunction(items:ArrayCollection):Object
{
    var retVal:Object;
    for (var i:int = 0; i < items.length; i++)
    {
        var value:Object = items.getItemAt(i);
        if (value.name == this.attribute.value.directValue as String)
        {
            retVal = value;
            break;
        }

        if (value.hasOwnProperty("children"))
        {
                this.recursiveFindFunction(value.children);
        }   
    }

    return retVal;
}  
4

1 回答 1

1

您没有在任何地方捕捉到递归调用的返回

您没有在这里检查返回值

 if (value.hasOwnProperty("children"))
    {
            this.recursiveFindFunction(value.children);
    }   

一个可能的解决方法是添加这样的 return 语句:

 if (value.hasOwnProperty("children"))
    {
            return this.recursiveFindFunction(value.children);
    } 

(注意退货)

于 2012-08-03T10:00:08.670 回答