合并两个 ArrayCollection - Flex

发布于 2024-12-08 19:51:28 字数 1147 浏览 0 评论 0原文

我有两个 ArrayCollection,我想将它们合并为一个...

arr1 = 
[0] -> month = 07
       tot_err = 15
[1] -> month = 08
       tot_err = 16
[2] -> month = 09
       tot_err = 17


arr2 = 
[0] -> month = 07
       tot_ok = 5
[1] -> month = 08
       tot_ok = 6
[2] -> month = 09
       tot_ok = 7

我想要这个数组

arr3 = 
[0] -> month = 07
       tot_err = 15
       tot_ok = 5
[1] -> month = 08
       tot_err = 16
       tot_ok = 6
[2] -> month = 09
       tot_err = 17
       tot_ok = 7    

我该怎么做?

编辑:

我做了这个解决方案:

        private function mergeArrays(a:ArrayCollection, b:ArrayCollection):ArrayCollection
        {
            for (var i:int=0;i<a.length;i++)
                for each(var item:Object in b)
                {                   
                    if( a[i].month == item.month){
                         a[i].tot_err = item.tot_err;
                    }
                }
            return a;
        }

但是有一个重要的问题,如果 array2 (b) 有一个 item.month 而 array1 (a) 值丢失了...

I have two ArrayCollection and I want to merge them into one...

arr1 = 
[0] -> month = 07
       tot_err = 15
[1] -> month = 08
       tot_err = 16
[2] -> month = 09
       tot_err = 17


arr2 = 
[0] -> month = 07
       tot_ok = 5
[1] -> month = 08
       tot_ok = 6
[2] -> month = 09
       tot_ok = 7

I would like to have this array

arr3 = 
[0] -> month = 07
       tot_err = 15
       tot_ok = 5
[1] -> month = 08
       tot_err = 16
       tot_ok = 6
[2] -> month = 09
       tot_err = 17
       tot_ok = 7    

How can I do it?

EDIT:

I did this solution:

        private function mergeArrays(a:ArrayCollection, b:ArrayCollection):ArrayCollection
        {
            for (var i:int=0;i<a.length;i++)
                for each(var item:Object in b)
                {                   
                    if( a[i].month == item.month){
                         a[i].tot_err = item.tot_err;
                    }
                }
            return a;
        }

But there is an important problem, if array2 (b) has a item.month that there isn't in the array1 (a) the value is lost...

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

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

发布评论

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

评论(4

醉酒的小男人 2024-12-15 19:51:29
private function mergeArrays(a:ArrayCollection, b:ArrayCollection):ArrayCollection
{
    var result:ArrayCollection = new ArrayCollection();
    var months:Dictionary = new Dictionary();
    for (var i:int = 0; i < a.length; i++)
    {
        var mergedItem:Object = new Object();
        mergedItem.month = a[i].month;
        mergedItem.tot_ok = a[i].tot_ok;
        mergedItem.tot_err = null;
        for (var j:int = 0; j < b.length; j++)
        {                   
            if(a[i].month == b[j].month)
            {
                mergedItem.tot_err = b[j].tot_err;
            }
        }
        month[mergedItem.month] = true;
        result.addItem(mergedItem);
    }
    // so far we have handled all occurrences between a and b,
    // now we need to handle the items from b that are left
    for each (var bItem:Object in b)
    {
        mergedItem = new Object();
        mergedItem.month = bItem.month;
        mergedItem.tot_err = bItem.tot_err;
        mergedItem.tot_ok = null;
        if (months[mergedItem.month] == null)
        {
            month[mergedItem.month] = true;
            result.addItem(mergedItem);
        }
    }
    return result;
}
private function mergeArrays(a:ArrayCollection, b:ArrayCollection):ArrayCollection
{
    var result:ArrayCollection = new ArrayCollection();
    var months:Dictionary = new Dictionary();
    for (var i:int = 0; i < a.length; i++)
    {
        var mergedItem:Object = new Object();
        mergedItem.month = a[i].month;
        mergedItem.tot_ok = a[i].tot_ok;
        mergedItem.tot_err = null;
        for (var j:int = 0; j < b.length; j++)
        {                   
            if(a[i].month == b[j].month)
            {
                mergedItem.tot_err = b[j].tot_err;
            }
        }
        month[mergedItem.month] = true;
        result.addItem(mergedItem);
    }
    // so far we have handled all occurrences between a and b,
    // now we need to handle the items from b that are left
    for each (var bItem:Object in b)
    {
        mergedItem = new Object();
        mergedItem.month = bItem.month;
        mergedItem.tot_err = bItem.tot_err;
        mergedItem.tot_ok = null;
        if (months[mergedItem.month] == null)
        {
            month[mergedItem.month] = true;
            result.addItem(mergedItem);
        }
    }
    return result;
}
殤城〤 2024-12-15 19:51:29
if( a[i].month == item.month){
   a[i].tot_err = item.tot_err;
   // remove the item from b here. dontknow arraycollection
   // should be like b.remove(item);
}

在 for 循环之后,您可以检查“b”是否仍然有元素,因此您可以将它们添加到“a”,不要忘记为“b”数组对象提供默认的 tot_ok 值。另一件事是,如果“a”中的对象在“b”中没有等价物,则可以使用它。

    private function mergeArrays(a:ArrayCollection, b:ArrayCollection):ArrayCollection
    {
        var ex:Boolean = true;
        for (var i:int=0;i<a.length;i++){
            for each(var item:Object in b)
            {                   
                if( a[i].month == item.month){
                     a[i].tot_err = item.tot_err;
                     ex = true;
                }else{
                     ex = false;
                }
            }
            if(!ex){
               // give a default value here.
               a[i].tot_err = 0;
            }
        }


        return a;
    }
if( a[i].month == item.month){
   a[i].tot_err = item.tot_err;
   // remove the item from b here. dontknow arraycollection
   // should be like b.remove(item);
}

after for loops you can check if "b" still have elements, so you can add them to "a", dont forget to give "b" array objects a default tot_ok value. And another thing if an object in "a" that doesnt have equalivant in "b" you can use this.

    private function mergeArrays(a:ArrayCollection, b:ArrayCollection):ArrayCollection
    {
        var ex:Boolean = true;
        for (var i:int=0;i<a.length;i++){
            for each(var item:Object in b)
            {                   
                if( a[i].month == item.month){
                     a[i].tot_err = item.tot_err;
                     ex = true;
                }else{
                     ex = false;
                }
            }
            if(!ex){
               // give a default value here.
               a[i].tot_err = 0;
            }
        }


        return a;
    }
┾廆蒐ゝ 2024-12-15 19:51:29
private function mergeArrayCollections(a:ArrayCollection, b:ArrayCollection):ArrayCollection {
    var c:ArrayCollection=new ArrayCollection(b.toArray()); //clone b so as not to modify b
    //This loop handles all objects common to a and b
    for each(var o:Object in a) {
        for (var i:int=0; i<c.length; i++) {
            var p:Object=c.getItemAt(i);
            if(o.month==p.month) {
                //if the month is the same then add the property to a
                o.tot_ok=p.tot_ok;
                c.removeItemAt(i);
                break;
            }
        }
    }
    //This loop adds the leftover items from c to a
    for each(var q:Object in c) {
        q.tot_err=-1; //add this so that all objects in a are uniform
        a.addItem(q);
    }
    return a; //Unnecessary return, a will be modified by reference
}
private function mergeArrayCollections(a:ArrayCollection, b:ArrayCollection):ArrayCollection {
    var c:ArrayCollection=new ArrayCollection(b.toArray()); //clone b so as not to modify b
    //This loop handles all objects common to a and b
    for each(var o:Object in a) {
        for (var i:int=0; i<c.length; i++) {
            var p:Object=c.getItemAt(i);
            if(o.month==p.month) {
                //if the month is the same then add the property to a
                o.tot_ok=p.tot_ok;
                c.removeItemAt(i);
                break;
            }
        }
    }
    //This loop adds the leftover items from c to a
    for each(var q:Object in c) {
        q.tot_err=-1; //add this so that all objects in a are uniform
        a.addItem(q);
    }
    return a; //Unnecessary return, a will be modified by reference
}
七色彩虹 2024-12-15 19:51:29

如果 array2 (b) 具有 array1 (a) 中没有的 item.month,则使用 addItem() 方法将新对象添加到 array1 (a) ;

if array2 (b) has a item.month that there isn't in the array1 (a) use addItem() method to add new object to array1 (a) ;

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