4 Replies Latest reply on Jul 4, 2009 11:08 AM by kglad

    seek and destroy unknown array elements

    shintashi Level 1


      Hi, I'm looking for a way to determine if my array contains a specific element (without knowing what position number it is in the array)based on it's content, and then splices/deletes it.

       

      I found this from kirupa:

      ----

      Array.prototype.contains = function(input){
              for (var i in this) {
                      if (this[i] == input) {
                              return 1;
                      }
              }
      return 0;
      };
      //usage
      myArray.contains("value");

      ----

       

      and figure I could use something like it with
      myArray.splice(target,1);

       

      the only problem I'm seeing is getting "target" in the splice command to equal the "input" in the myArray.contains(); function.

       


      I originally needed this because I'm trying to add elements with prerequisites or associations intact, and subsequently when deleting the prerequsite for several other elements, those elements would also be deleted.

        • 1. Re: seek and destroy unknown array elements
          Ned Murphy Adobe Community Professional & MVP

          The splice method requires the starting index of the items to remove, so you need to obtain the index of the tested element in the array.  If you change the prototype as follows it will return the index of the found element, if it is found, otherwise, -1.

           

          Array.prototype.contains = function(input){
                  for (var i in this) {
                          if (this[i] == input) {
                                  return i;
                          }
                  }
          return -1;
          };

           

          You would need to test the return value to use it with splice because splice will still work for negative values.

           

          var indexNum = myArray.contains("value");

           

          if(indexNum >= 0){

               myArray.splice(indexNum, 1);

          }

          • 2. Re: seek and destroy unknown array elements
            Rothrock Level 5

            BTW when you add a prototype to the built in Array  function you need to add the following line just after the prototype definition:

             

            ASSetPropFlags(Array.prototype,"contains",1,false);

            The prototype definition gets added to every array and if you do a for..in loop it will have one extra item of [type function] there are times when that mess you up. That code hides the new method (function) from the tyranny of the for..in loop.
            Might not have caused you trouble yet, but it will at some point.

            • 3. Re: seek and destroy unknown array elements
              shintashi Level 1

              last night/1am this morning before passing out I wrote this demo up to test if the idea was even going to work.

               

               

              -----

              Array.prototype.getIndex = function(data) {
                  for (i=0; i<this.length; ++i) {
                      if (this[i] == data) {
                          return i;
                      }
                  }
                  return -1;
              };

               


              var afruit:Array = new Array(3);
              afruit[0] = "apple";
              afruit[1] = " orange";
              afruit[2] = "banana";

               

              swap = 0;

               

              onEnterFrame = function(){
              fruit.text = afruit.toString();

               


              swapup.onPress = function(){if(swap < 2){swap++;}}
              swapdown.onPress = function(){if(swap > 0){swap--;}}

               

              fruitlist.text = afruit[swap];

               

              erase.onPress = function(){

               

              if(fruitlist.text == String(" orange")){afruit.splice(1,1);
              seeker = afruit.getIndex("apple"); outpuut.text = afruit.splice(seeker,1);
                  }

               

              }

               

              }

              --------

              but I don't have a clue why essentially the same program doesn't work for my larger arrays. I also feel this version is rather cumbersome and would prefer my function contain the splice (but don't know how). In the large program I have this prerequisite system rigged so if you add certain elements to a list, prerequisite elements are added as well, using the format:

               

              addmcI.onPress = function(){
              if(mylistedelement == "ElementwithPrerequisite"){myArray.push(" "+ "Prerequisite");}

               

              I'm seriously wondering if the reason my fruit list works and my main program doesn't is because of the " "+ added.

              • 4. Re: seek and destroy unknown array elements
                kglad Adobe Community Professional & MVP

                onEnterFrame functions repeatedly execute the code within their function body.  fix that.