4 Replies Latest reply on Jan 14, 2010 1:18 AM by Francisc

    ArrayCollection help

    Francisc Level 3

      Hello!

       

      1. I have an ArrayCollection that has 2 objects in it, a string and an object which is an Image. What I need is when the image is clicked (it's displayed) to show an alert box with the text. So... is there anyway to backtrack from event.target to the index of the ArrayCollection that image is in please?

       

      2. I have an array collection that has elements like:

      myAC[0][label], myAC[0][data],

      myAC[1][label], myAC[1][data],

      myAC[2][label], myAC[2][data],

      etc.

       

      I run a for that takes the unique values of label and saves them into another ArrayCollection like this:

       

      for(var i:int=0;i<myAC.length;i++)

      {

           if(myUniqueAC.contains(myAC[i]['label']

                myUniqueAC.addItem(myAC[i]);

      }

       

      However, that doesn't work because contains does not look into the whole ArrayCollection.

      Is there another way to do this then running another for and comparing myUniqueAC[j][label].contains(...) ?

       

      Thank you!

        • 1. Re: ArrayCollection help
          Flex harUI Adobe Employee

          I think I would do:

           

          Var labels:Object = new Object();

          For (I = 0; I < AC.length; i++)

          {

              var lbl:String = ac[i].label;

              labels[lbl] = 1;

          }

          Var labelList:Array = new Array();

          For each (lbl in labels)

              labelList.push(lbl);

          • 2. Re: ArrayCollection help
            Francisc Level 3

            Not sure if I should mark your answer as Helpful, Correct or throw a rotten tomatoe at it... Haha.

             

            So... what you do is:

            //create a new Object

            Var labels:Object = new Object();

            //run a for to assign the value 1 to the newly created Object having the key named after the values, thus only keeping unique keys

            For (I = 0; I < AC.length; i++)

            {

                var lbl:String = ac[i].label;

                labels[lbl] = 1;

            }

            //create a new Array

            Var labelList:Array = new Array();

            //run a for to assign the keys into the array thus having only unique ones

            For each (lbl in labels)

                labelList.push(lbl);

             

            It's a very clever way to do this, but isn't it too much code and too many instructions (execution-time wise) ?

            There are also 2 other variables created in the memory space.

             

            Is there no other way?

            the for in for method with iterative i and j vars seems faster I think.

            Isn't there a way to implement the .contains method so that it does what I want?

             

            Sorry for all the questions and thank you!

            • 3. Re: ArrayCollection help
              Flex harUI Adobe Employee

              Your mileage may vary, but contains is going to do a linear search so I

              think my suggestion is faster as it uses a hash to eliminate duplicates.

              The profiler or timer tests will tell you which way to go.

              • 4. Re: ArrayCollection help
                Francisc Level 3

                Alright, I'll do it `your way`.

                But if the search has to be done more in-depth than [i][label], for example [i][label][type][name] or on multiple elements. for example: [i][label][type] amd [i][label][name][location] ?

                 

                (the labels are what popped to mind now, don't mind if they don't make sense as a good data structure)