2011-10-06 51 views
0

我有兩個ArrayCollection的,我想將它們合併成一個...合併兩個ArrayCollection中 - 的Flex

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

但有一個重要的問題,如果數組2(b)具有item.month有沒有在陣列1(a)的值丟失...

+0

問題和您的解決方案是不明確的。 – ymutlu

+0

你不明白什麼? – Marcx

+0

啊你固定了,現在這個意思。我會檢查出來 – ymutlu

回答

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

如果數組2(b)的item.month不存在的陣列1(A)使用的addItem()方法來添加新的對象陣列1(一);

+0

如果第一次不會返回true,它會將b中的所有對象放到a中。 – ymutlu

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

後循環您可以檢查是否「B」還有元素,所以你可以將它們添加到「一」,不要忘了給「B」數組對象的默認tot_ok值。還有一件事,如果在「一」,這並不一個對象在「B」已經equalivant您可以使用此。

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