Arrays as3检查数组中具有相同属性的2个对象
我有一个数组,我们称之为_persons。 我用值对象填充这个数组,让我们调用这个对象PersonVO 每个PersonVO都有一个名称和一个score属性 我要做的是搜索数组&Arrays as3检查数组中具有相同属性的2个对象,arrays,actionscript-3,compare,Arrays,Actionscript 3,Compare,我有一个数组,我们称之为_persons。 我用值对象填充这个数组,让我们调用这个对象PersonVO 每个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
//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向导能够提供帮助?您希望在阵列中循环,并检查是否有两个同名的人 我有另一个可能有用的解决方案,如果没有,请说
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,
childrenstate=this.numChildren;
var aPerson:数组=新数组;
for(变量c:int=0;c
您最好使用字典来执行此任务,因为您有一个指定的唯一属性要查询。如果您只有一个键属性,那么字典方法是可行的,在您的案例中,名称
,并且您在任何给定时间只需要一个对象就可以拥有此属性。例如:
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;
}
var高分:字典;
//用某种方式加载它
函数addHighscore(名称:字符串,分数:数字):布尔值{
//如果此分数大于存储的值(也称为个人最佳值),则返回true
var prevScore:编号=高分[名称];
如果(isNaN(prevScore)| |(prevScore