7 Replies Latest reply on May 10, 2009 8:40 AM by ShaF10

    Search array

    ShaF10 Level 1

      Hi Guys

       

      I have an array with afew objects in it. How do I search it ? code below:

       

      var myArray:Array = new Array();

       

      //create new obj and insert into array

      var newObj:MyClass = new MyClass();

      var newObj2:MyClass = new MyClass();

       

      myArray.push(newObj);

      myArray.push(newObj2);

        • 1. Re: Search array
          run,ryan! Level 3

          myArray.indexOf()

          • 2. Re: Search array
            ShaF10 Level 1

            Thanks ryan. That returns an int right ? What Im trying to do is search through the list and if the index number matches the given ID then that object should be retrieved. In Java I would use a for loop for this, I have tried the same with AS3 to no avail. code below:

             

            for(var i:int; i < myArray.length; i++)
            {
                if(myArray.indexOf(i) == id) {
                    // how do I retrieve the object ?
                }
            }

            • 3. Re: Search array
              Michael Borbor Level 4

              If that what you want the I could recommend you to use an ArrayCollection

              and then make a search to the AC using getItemIndex.

              1 person found this helpful
              • 4. Re: Search array
                ShaF10 Level 1

                I tried that but whenever the loop is run the item index keeps returning -1 even though it has several objects in it. See code below:

                 

                var myObj:newObj;

                 

                for(var i:int; i < myArray.length; i++)
                {
                    trace("running loop. Item index is " + myArray.getItemIndex(i) + " and id is " + id);
                    if(myArray.getItemIndex(i) == id) {
                        myObj = myArray[i];
                    }
                }

                 

                This is what trace returns:

                 

                running loop. document item index is -1 and id is 1

                running loop. document item index is -1 and id is 2

                • 5. Re: Search array
                  Michael Borbor Level 4

                  I think you're misunderstanding the usage, try this snippet.

                   

                  var myDP:ArrayCollection = new ArrayCollection(
                                  ['A','B','C','D','E']);
                                 Alert.show(myDP.getItemIndex('C').toString());
                  
                  1 person found this helpful
                  • 6. Re: Search array
                    ShaF10 Level 1

                    Ok, that'll work for chars but Im talking about Objects. This is what I am trying to do: add 10 objects using the addItem() property, and at a later date retrieve object 6 from the array.

                     

                    Message was edited by: ShaF10

                    • 7. Re: Search array
                      Michael Borbor Level 4

                      Oh I'm sorry I didn't read carefully your post I created an small test case,maybe it'll help you.

                       

                      private function testObjects():void{
                                     var myNewArtist:ARTISTS;
                                     for(var a:Number=0;a<10;a++){
                                          myNewArtist=new ARTISTS();
                                          myNewArtist.ARTISTID=a+1;
                                          myAC.addItem(myNewArtist);
                                     }
                                     var mySearchCriteria:ARTISTS=ARTISTS(myAC.getItemAt(5));
                                     
                                     Alert.show(myAC.getItemIndex(mySearchCriteria).toString());
                                }
                      

                      And I'm using a VO

                       

                      package 
                      {
                           [RemoteClass(alias="ARTISTS")]
                      
                           [Bindable]
                           public class ARTISTS
                           {
                      
                                public var ARTISTID:Number = 0;
                                public var FIRSTNAME:String = "";
                                public var LASTNAME:String = "";
                      
                      
                                public function ARTISTS()
                                {
                                }
                      
                           }
                      }