2014-04-29 39 views
0

我有一個數組,我們可以稱它爲_persons。 我正在使用Value Objects填充此數組,我們稱該對象PersonVOas3檢查數組中具有相同屬性的2個對象

每個PersonVO都有一個名稱和一個score屬性。

我所試圖做的是搜索陣列&

//PSEUDO CODE 

1 Find any VO's with same name (there should only be at most 2) 
2 Do a comparison of the score propertys 
3 Keep ONLY the VO with the higher score, and delete remove the other from the _persons array. 

我在用代碼實現的麻煩。任何AS3嚮導能夠提供幫助?

回答

0

你最好使用Dictionary這個任務,因爲你有一個指定的唯一屬性進行查詢。如果你只有一個關鍵屬性,字典方法是可行的,在你的情況下name,你需要在任何給定的時間只有一個對象擁有這個屬性。舉個例子:

var highscores:Dictionary; 
// load it somehow 
function addHighscore(name:String,score:Number):Boolean { 
    // returns true if this score is bigger than what was stored, aka personal best 
    var prevScore:Number=highscores[name]; 
    if (isNaN(prevScore) || (prevScore<score)) { 
     // either no score, or less score - write a new value 
     highscores[name]=score; 
     return true; 
    } 
    // else don't write, the new score is less than what's stored 
    return false; 
} 

在這個例子中詞典採用傳遞的字符串作爲name屬性,即「主鍵」在這裏,因此,所有記錄應具有獨特的name一部分,傳遞給函數。得分是存儲記錄的值部分。您可以將多個屬性存儲在字典中作爲值,在這種情況下,您需要將其包裝爲Object

0

你想循環數組並檢查是否有任何兩個同名的人。

我有另一種解決方案,可能會有所幫助,如果不是請說說。

    childrenOnStage = this.numChildren; 
        var aPerson:array = new array; 


     for (var c:int = 0; c < childrenOnStage; c++) 
     { 
      if (getChildAt(c).name == "person1") 
      { 
       aPerson:array =(getChildAt(c); 
      } 
     } 

     Then trace the array, 
相關問題