2014-04-29 41 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, 
相关问题