12 Replies Latest reply on Nov 12, 2006 9:28 PM by kglad

    Removing Undefined elements in arrays

    VinceVaughn
      Anyone who can help, will go down in history as my (pretty much) ultimate hero...

      Ok, here's the deal: I'm populating a combobox with data from an array. The array is created dynamically from an XML file. It all works beautifully, except for that I have some undefined values in my array because the parent nodes in the xml file won't allways have the particular child node that builds the array.

      So what I get is, a combobox with a "blank" element here and there - whenever I have an undefined value in my array. So, I just need to scrub out the undefined elements from my array that populates the combobox. BUT I HAVE NO FREAKIN' CLUE HOW TO DO THAT AND I'M PULLING MY HAIR OUT AND ROCKING IN THE FETAL POSITION MUMBLING TO MYSELF!!!

      PLEASE HELP. How do I remove the undefined values from an array? Anyone?
        • 1. Re: Removing Undefined elements in arrays
          Level 7

          "VinceVaughn" <webforumsuser@macromedia.com> wrote in message
          news:ej30fj$ab4$1@forums.macromedia.com...
          > Anyone who can help, will go down in history as my (pretty much) ultimate
          > hero...
          >
          > Ok, here's the deal: I'm populating a combobox with data from an array.
          > The
          > array is created dynamically from an XML file. It all works beautifully,
          > except for that I have some undefined values in my array because the
          > parent
          > nodes in the xml file won't allways have the particular child node that
          > builds
          > the array.
          >
          > So what I get is, a combobox with a "blank" element here and there -
          > whenever
          > I have an undefined value in my array. So, I just need to scrub out the
          > undefined elements from my array that populates the combobox. BUT I HAVE
          > NO
          > FREAKIN' CLUE HOW TO DO THAT AND I'M PULLING MY HAIR OUT AND ROCKING IN
          > THE
          > FETAL POSITION MUMBLING TO MYSELF!!!
          >
          > PLEASE HELP. How do I remove the undefined values from an array? Anyone?
          >

          var myArray:Array = new Array("1", undefined, "3", 4, 5, "undefined", "7",
          undefined, "9", 10);
          function removeUndefined(myArray:Array):Array {
          for (var i:Number = 0; i < myArray.length; i++) {
          if (myArray == undefined) {
          myArray.splice(i, 1);
          }
          }
          return myArray;
          }
          trace(myArray);
          trace(removeUndefined(myArray));


          • 2. Re: Removing Undefined elements in arrays
            VinceVaughn Level 1
            hmmmm...thanks for the quick reply, however, did this code that you provided work for you?

            I ask because I pasted the code exactly as you provided into a new flash file and the two traces are identical in the output panel, leading me to think that it's not working? Or am I missing something?
            • 3. Re: Removing Undefined elements in arrays
              kglad Adobe Community Professional & MVP
              i don't know why you would need to consider the possibility that a variable contains the string "undefined", but if you do you'll need a little more than smb's code:

              • 4. Re: Removing Undefined elements in arrays
                Level 7

                "kglad" <webforumsuser@macromedia.com> wrote in message
                news:ej33oj$dqq$1@forums.macromedia.com...
                >i don't know why you would need to consider the possibility that a variable
                > contains the string "undefined", but if you do you'll need a little more
                > than
                > smb's code:
                >
                >
                >
                > var myArray:Array = new Array("1", undefined, "3", 4, 5, "undefined", "7",
                > undefined, "9", 10);
                > function removeUndefined(myArray:Array):Array {
                > for (var i:Number = 0; i<myArray.length; i++) {
                > if (myArray == undefined || myArray == "undefined") {
                > myArray.splice(i, 1);
                > }
                > }
                > return myArray;
                > }
                > trace(myArray);
                > trace(removeUndefined(myArray));
                >


                kglad... I don't think the OP wanted to exclude strings with "undefined", I
                was just doing it to show that the function only removes truly undefined
                variables.


                • 5. Re: Removing Undefined elements in arrays
                  Level 7

                  "VinceVaughn" <webforumsuser@macromedia.com> wrote in message
                  news:ej3348$d5p$1@forums.macromedia.com...
                  > hmmmm...thanks for the quick reply, however, did this code that you
                  > provided
                  > work for you?
                  >
                  > I ask because I pasted the code exactly as you provided into a new flash
                  > file
                  > and the two traces are identical in the output panel, leading me to think
                  > that
                  > it's not working? Or am I missing something?
                  >

                  These are my two traces:
                  1,undefined,3,4,5,undefined,7,undefined,9,10
                  1,3,4,5,undefined,7,9,10


                  • 6. Re: Removing Undefined elements in arrays
                    VinceVaughn Level 1
                    BLESS YOU KGLAD!!!!

                    And thanks to you too SMB!
                    • 7. Re: Removing Undefined elements in arrays
                      Level 7

                      "VinceVaughn" <webforumsuser@macromedia.com> wrote in message
                      news:ej343h$e4t$1@forums.macromedia.com...
                      > BLESS YOU KGLAD!!!!
                      >
                      > And thanks to you too SMB!

                      kglad gets blessed, and I get a thanks... hmmm.


                      • 8. Re: Removing Undefined elements in arrays
                        kglad Adobe Community Professional & MVP
                        oh. if you don't want to remove strings, eliminate the or ( || ) part of that if-statement.
                        • 9. Re: Removing Undefined elements in arrays
                          kglad Adobe Community Professional & MVP
                          lol. yeah, that wasn't very fair.

                          at least i recognize you were the major contributor to this answer. (but use the attach code option so your answer is readable.)
                          • 10. Re: Removing Undefined elements in arrays
                            Level 7

                            "kglad" <webforumsuser@macromedia.com> wrote in message
                            news:ej3ran$7n6$1@forums.macromedia.com...
                            > lol. yeah, that wasn't very fair.
                            >
                            > at least i recognize you were the major contributor to this answer. (but
                            > use the attach code option so your answer is readable.)


                            kglad,

                            didn't realize my answers weren't readable. I use standard news readers for
                            to respond here and all of the code I post always looks fine. How does it
                            come out on your end?

                            Where do you do your posts from? Adobe's site?

                            Thanks.


                            • 11. Re: Removing Undefined elements in arrays
                              fungusbogus
                              If the XML tag is empty (I.E - There is no text in the tag) then you could try set
                              XML.ignoreWhite = True:

                              Does this work?
                              • 12. Re: Removing Undefined elements in arrays
                                kglad Adobe Community Professional & MVP
                                when you use [ i ] in your message this forum interprets that as encoding for italic font. which wouldn't be more than a nuissance but it means everytime you use [ i ], we don't see the left bracket, i and right bracket. so, your array element references are incorrect.