as3 ตรวจสอบ 2 วัตถุที่มีคุณสมบัติเหมือนกันในอาร์เรย์

ฉันมีอาร์เรย์ ลองเรียกมันว่า _persons ฉันกำลังเติมอาร์เรย์นี้ด้วย Value Objects ให้เรียกวัตถุนี้ว่า PersonVO

PersonVO แต่ละคนมีชื่อและคุณสมบัติคะแนน

สิ่งที่ฉันพยายามทำคือค้นหาอาร์เรย์ &

//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 ใดบ้างที่สามารถช่วยได้


person Bachalo    schedule 29.04.2014    source แหล่งที่มา


คำตอบ (2)


คุณควรใช้ 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 ในกรณีนี้

person Vesper    schedule 29.04.2014

คุณต้องการวนซ้ำอาร์เรย์และตรวจสอบว่ามีคนสองคนที่มีชื่อเดียวกันหรือไม่

ฉันมีวิธีแก้ปัญหาอื่นที่อาจช่วยได้ ถ้าไม่กรุณาพูด

                    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, 
person Moynul    schedule 29.04.2014