3 Replies Latest reply on Dec 31, 2010 6:41 AM by Fayena

    Do some of the array commands not work with associative arrays?

    Fayena Level 1

      I have a couple of associative arrays that I would like to use a slice() and length on but they don't appear to be working.  When a do a trace(array.length) it gives me 0 even though immediately after I do a for loop and trace all the elements and its defiantly not zero.  Also the array2.slice(j,1) does not work in the following example but delete array2[j] does although its not exactly what I want.  What am I missing?   

       

      for (var j:String in array2) {

       

                if (i == j) {

                trace (i + j);

                array1[i] = array1[i] + array2[j];

                array2.slice(j,1);

                trace("array2:"+array2.length);

                for (var x:String in array2) {

                     trace(x);

       

                }

       

       

      Thanks for the help.

        • 1. Re: Do some of the array commands not work with associative arrays?
          BhaskerChari Level 4

          Hi,

           

          The slice method will not delete/modify elements in the original/source Array. In order to remove/insert elements you can make use of splice method of Array instead.

           

          Also remember that array2.slice(j,1); returns the Array containing the elements you want to filter but doesn't modify the source Array.

           

          array2 = array2.slice(j,1);

           

          Just read through the slice() and splice() methods of Array.

           

          Thanks,

          Bhasker

          • 2. Re: Do some of the array commands not work with associative arrays?
            Fayena Level 1

            Sorry, I had been trying to use splice to remove the elements but that hadn't worked so I had change it slice and forgot to change it back when I posted.

             

            I have been using this from the Resource Center

            First, the splice() method is used to remove the items Aral and Superior, and insert the items Atlantic and Indian. The first argument sent to splice(), the integer 2, indicates that the operation should start with the third item in the list, which is at index 2. The second argument, 2, indicates that two items should be removed. The remaining arguments, Atlantic and Indian, are values to be inserted at index 2.

            Second, the pop() method is used to remove last element in the array, Huron. And third, the shift() method is used to remove the first item in the array, Victoria.

            var oceans:Array = ["Victoria", "Pacific", "Aral", "Superior", "Indian", "Huron"];
            oceans.splice(2, 2, "Arctic", "Atlantic"); // replaces Aral and Superior
            oceans.pop(); // removes Huron
            oceans.shift(); // removes Victoria
            trace(oceans);// output: Pacific,Arctic,Atlantic,Indian
            

            So I'm getting out of this that I should be able to use array2.splice(j,1)  and that should work the same as delete for my purposes but it doesn't. 

             

            Here is my code:

             

            for (var i:String in array1) {

                   break2: for (var j:String in array2) {

                      if (i == j) {

                           array1[i] = array1[i] + array2[j];

                           //delete array2[j];

                           array2.splice(j, 1);

                           break break2;

                      }//end if

                      }//end inner for

                      trace("break");

                      for (var p:String in array2) {

                           trace("in merge"+p);

                      }

                      trace("break");

            }//end outer for

             

            With the delete array2[j] in the code and the splice commented out I get this from trace

             

            in mergeEE,aa

            in mergeee,AA

            in mergeee,aa

            in mergeEE,Aa

            break

            break

            in mergeee,AA

            in mergeee,aa

            in mergeEE,Aa

            break

            break

            in mergeee,aa

            in mergeEE,Aa

            break

            break

            in mergeEE,Aa

            break

            break

            which is what I want so all is good.  However, when I try to use the splice method...
            break
            break
            in mergeEE,aa
            in mergeee,AA
            in mergeee,aa
            in mergeEE,Aa
            break
            break
            in mergeEE,aa
            in mergeee,AA
            in mergeee,aa
            in mergeEE,Aa
            break
            break
            in mergeEE,aa
            in mergeee,AA
            in mergeee,aa
            in mergeEE,Aa
            Also, if I add the code trace("length:"+array2.length);  right before the trace loop all I get for length is 0.  I understand that delete does not change the length of the array but it should return something other than 0?
            Thank you for your time and patience.

            • 3. Re: Do some of the array commands not work with associative arrays?
              Fayena Level 1

              I found the answer.  I had skimmed through this page but evidently just read the examples that said I could make associative arrays. Completely missed this sentence. 

               

              You cannot use the Array.length property or any of the methods of the Array class with associative arrays, even if you use the Array constructor or the Array data type.