Слияние двух ArrayCollection - Flex - PullRequest
       32

Слияние двух ArrayCollection - Flex

0 голосов
/ 06 октября 2011

У меня есть две коллекции 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    

Как я могу это сделать?

EDIT:

Я сделал это решение:

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

Но есть важная проблема, если массив2 (b) имеет item.month, которого нет в массиве1 (a), значение теряется ...

Ответы [ 4 ]

5 голосов
/ 06 октября 2011
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;
}
0 голосов
/ 06 октября 2011
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
}
0 голосов
/ 06 октября 2011
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;
    }
0 голосов
/ 06 октября 2011

, если для array2 (b) есть item.month, которого нет в array1 (a), используйте метод addItem () для добавления нового объекта в array1 (a);

...