ArrayCollection 中项目的深度

发布于 2024-08-26 08:32:41 字数 42 浏览 3 评论 0原文

是否可以以某种方式获取 ArrayCollection 中项目的深度?

is it somehow possible to get item's depth in ArrayCollection?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

丑疤怪 2024-09-02 08:32:41

来自 livedocs

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

From the livedocs:

// Get the index of the item with the value ME.
var addedItemIndex:int=myAC.getItemIndex("ME");
夏尔 2024-09-02 08:32:41

这是我的代码...

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;
    }

here is my code...

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;
    }
终止放荡 2024-09-02 08:32:41

不是答案,我无法发表评论。

来自实时文档: http://livedocs.adobe.com/ flex/3/langref/mx/collections/ArrayCollection.html

ArrayCollection 类是一个包装器
将数组公开为的类
可以访问的集合以及
使用方法和操作
ICollectionView 的属性或
IList接口。

为什么你认为 ArrayCollection 有深度?

我想你可以创建一个子 ArrayCollections 的 ArrayCollection。如果是这种情况;那么您可以编写一个函数来搜索其所有子 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;
}

Not an answer, I can't post comments.

From live docs: http://livedocs.adobe.com/flex/3/langref/mx/collections/ArrayCollection.html

The ArrayCollection class is a wrapper
class that exposes an Array as a
collection that can be accessed and
manipulated using the methods and
properties of the ICollectionView or
IList interfaces.

Why do you think an ArrayCollection has depth?

I guess you can make an ArrayCollection of sub-ArrayCollections. If this is the case; then you could write a function that searches through all its sub-ArrayCollections.

EDIT: I think there are some bugs in the function you suggested. Here's a function I tried:

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文