对 XMLList 进行切片而不是数组

发布于 2024-10-15 18:19:59 字数 1063 浏览 5 评论 0原文

如何从 xmllist 中获取一系列项目,类似于数组的切片方法?

切片(起始索引,结束索引);

我正在尝试这样的事情:

            var tempXMLList:XMLList = new XMLList();

            for( var i:int = startIndex; i <= endIndex; i++){
                tempXMLList += originalList[i];
            }

但我收到一个错误,它无法转换originalList[i]

--- 更新 ---

我使用了 Timofei 的函数,它工作得很好。

private function SliceXMLList(xmllist : XMLList, startIndex : int, endIndex : int) : XMLList
{
    return xmllist.(childIndex() >= startIndex && childIndex() <= endIndex);
}

但是,当我使用已过滤的 xmllist 时,它会崩溃。

filteredData = filteredData.(team == "Ari");

trace("filteredData.length(): " + filteredData.length().toString());
pData = SliceXMLList(filteredData, startIndex, endIndex);
trace("start: " + startIndex.toString() + " end: " + endIndex.toString());
trace("pdata length: " + pData.length().toString());

输出

filteredData.length(): 55
start: 0 end: 55
pdata length: 5

How would I get a range of items from my xmllist similar to the slice method for an array?

slice(startIndex,endIndex);

I am trying something like this:

            var tempXMLList:XMLList = new XMLList();

            for( var i:int = startIndex; i <= endIndex; i++){
                tempXMLList += originalList[i];
            }

But I am getting an error that it can't convert originalList[i]

--- Update ---

I used Timofei's function and it worked perfectly.

private function SliceXMLList(xmllist : XMLList, startIndex : int, endIndex : int) : XMLList
{
    return xmllist.(childIndex() >= startIndex && childIndex() <= endIndex);
}

However, when I use an xmllist that was already been filtered, it breaks.

filteredData = filteredData.(team == "Ari");

trace("filteredData.length(): " + filteredData.length().toString());
pData = SliceXMLList(filteredData, startIndex, endIndex);
trace("start: " + startIndex.toString() + " end: " + endIndex.toString());
trace("pdata length: " + pData.length().toString());

output

filteredData.length(): 55
start: 0 end: 55
pdata length: 5

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

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

发布评论

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

评论(2

执手闯天涯 2024-10-22 18:19:59

使用 e4x。

private function SliceXMLList(xmllist : XMLList, startIndex : int, endIndex : int) : XMLList
{
    return xmllist.(childIndex() >= startIndex && childIndex() <= endIndex);
}

更新:

如果您打算在进行一些 e4x 排序后使用此函数,则会出现问题,因为 childIndex() 函数返回节点索引的旧值并且无法更改。所以,我有另一个想法:

private function SliceXMLList(xmllist : XMLList, startIndex : int, endIndex : int) : XMLList
{
    for (var i : int = 0; i < xmllist.length(); i++)
        xmllist[i].@realIndex = i;
    xmllist =  xmllist.(@realIndex >= startIndex && @realIndex <= endIndex);
    for (i = 0; i < xmllist.length(); i++)
        delete xmllist[i].@realIndex;
    return xmllist;
}

或者只是

private function SliceXMLList(xmllist : XMLList, startIndex : int, endIndex : int) : XMLList
{
    var newXMLList : XMLList = new XMLList();
    var currIndex : int = 0;
    for (var i : int = startIndex; i <= endIndex; i++)
        newXMLList[currIndex++] = xmllist[i];
    return newXMLList;
}

我认为这是最好的变体。当然,一行 e4x 语句要优雅得多,但不幸的是它不可重用。

Use e4x.

private function SliceXMLList(xmllist : XMLList, startIndex : int, endIndex : int) : XMLList
{
    return xmllist.(childIndex() >= startIndex && childIndex() <= endIndex);
}

Update:

There's a problem, if you're going to use this function after some e4x-sorting, 'cause the childIndex() function returns the old values of the nodes' indexes and it cannot be changed. So, I have another idea:

private function SliceXMLList(xmllist : XMLList, startIndex : int, endIndex : int) : XMLList
{
    for (var i : int = 0; i < xmllist.length(); i++)
        xmllist[i].@realIndex = i;
    xmllist =  xmllist.(@realIndex >= startIndex && @realIndex <= endIndex);
    for (i = 0; i < xmllist.length(); i++)
        delete xmllist[i].@realIndex;
    return xmllist;
}

or just

private function SliceXMLList(xmllist : XMLList, startIndex : int, endIndex : int) : XMLList
{
    var newXMLList : XMLList = new XMLList();
    var currIndex : int = 0;
    for (var i : int = startIndex; i <= endIndex; i++)
        newXMLList[currIndex++] = xmllist[i];
    return newXMLList;
}

This is the best variant, i think. Of course one-line e4x statement is much more elegant, but unfortunately it's not reusable.

关于从前 2024-10-22 18:19:59

不确定您期望它如何工作,但是您可以迭代所有子项,并将每个子项保存到一个数组中,然后以这种方式修剪它们

not sure how you expect it to work, but you could iterate through all children, and save each one into an array, and then trim them that way

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