2010-03-21 49 views

回答

0

这里是我的代码...

public function getItemNestLevel(needle:Object, haystack:Object, level:Number = 0):Number 
    { 
     //iterate through items 
     for each (var item:Object in haystack) 
     { 
      if (item == needle) 
      { 
       return level; 
      } 

      //iterate through item's properties 
      for each (var child:Object in item) 
      { 
       if (child is Array || child is ArrayCollection) 
       { 
        var lvl:Number = level + 1; 
        var num:Number = getItemNestLevel(needle, child, lvl); 
        if (num >= 0) 
        { 
         return num; 
        } 
       } 
      } 
     } 
     return -1; 
    } 
0

livedocs

// Get the index of the item with the value ME. 
var addedItemIndex:int=myAC.getItemIndex("ME"); 
+0

它不深,但指数...由“深度”我的意思是多少嵌套在ArrayCollection,其中项目树 – luccio 2010-03-21 16:45:37

0

不是一个答案,我不能发表评论。

从活文档:http://livedocs.adobe.com/flex/3/langref/mx/collections/ArrayCollection.html

ArrayCollection类是一个包装 类暴露数组作为 集合,可以访问和 使用方法及的ICollectionView的或 的IList的 特性操纵接口。

为什么你认为ArrayCollection具有深度?

我想你可以做一个ArrayCollection子ArrayCollections。如果是这样的话;那么你可以编写一个函数来搜索其所有子ArrayCollections。

编辑:我认为你建议的功能有一些错误。这里有一个功能我想:

public function getItemNestLevel2(needle:Object, haystack:Object):Number 
{ 
    for each (var item:Object in haystack) 
    { 
     if (item == needle) 
      return 0; 
     if (item is Array || item is ArrayCollection) 
     { 
      var nestLevel:int = getItemNestLevel2(needle, item); 
      if (nestLevel >= 0) 
       return nestLevel + 1; 
     } 
    } 
    return -1; 
} 
+0

我希望,已经有这样的功能。谢谢 – luccio 2010-03-22 08:16:03

+0

对于Flex的批评我听说过, Flex没有很棒的收藏类。 : - / – 2010-03-22 11:10:58