4 Replies Latest reply on Apr 17, 2008 3:20 AM by Starlover_jacob

    ArrayCollections

    J_Fraser
      Hey,

      I have the problem of needing to find specific values in an arraycollection.

      My arraycollection looks like this

      chartData.addItem({Sample: timeValue, Value: rate});

      the question being I need a way of finding an the index of the specific sample quickly. As this chartData arraycollection may be very large. I know the common way is a for loop to go through the values, but this could take way to much time.

      Is there something I'm missing?

      Thanks
        • 1. Re: ArrayCollections
          atta707 Level 2
          have your tried ArrayCollection.getItemIndex()?
          • 2. ArrayCollections
            bazjapan Level 1
            How big is the collection?
            You could use the array collection's filter function. Which is probably the best way.
            However, that said I usually loop the collection like this-


            public static function ArrayCollSearch(term:String, host:ArrayCollection, props:Array):ArrayCollection{
            var ret:ArrayCollection = new ArrayCollection();

            for(var i:Number = 0; i <host.length; i++){
            for(var j:Number = 0; j
            <props.length; j++){
            if(host [props[j]] != null){
            if(String(host
            [props[j]]).indexOf(String(term)) != -1 && ret.contains(host ) == false){

            ret.addItem(host
            );
            }
            }
            }
            }
            return ret;
            }
            • 3. Re: ArrayCollections
              m_hartnett Level 3
              If you have a reference to that object in the collection do as atta707 says and use getItemIndex(object).

              If you do not have a reference and need to search for a attribute match based on some query criteria that is when you need to interate through the collection one at a time looking for the match.

              If you have a extremely large AC then take a lesson from how Databases utilize indexes to tables.
              As long as the AC is in some sorted order you can create an object that contains range indexes to that AC.
              The first step in looking up the data would be to find the starting and ending ranges from the index object.
              This would at least limit your looping.

              However, unless the AC is extremely large I would just use a loop with a early exit until you see that performance is an issue.
              • 4. Re: ArrayCollections
                Starlover_jacob Level 1
                i used this:
                I fill the arraycollection from the database. Data represents the id from the database and label represents the licker text.
                The licker text is ordered by name, so the data label does not match the id of the arraycollection.

                So lets say i know the id of soda(1).
                I now cannot say myarraycollection.getItemAt(1). If i do it will get me rum.
                so i did the following:

                ------------------------------------------------------------------------------------------ -----------------------------