1 Reply Latest reply on Nov 4, 2012 8:27 AM by Ned Murphy

    array, string.replace equivalent

    TOverturf Level 1

      Hello

       

      What can I use as the .replace method on an Array?

        • 1. Re: array, string.replace equivalent
          Ned Murphy Adobe Community Professional & MVP

          The replace method of the String class does a couple of things.  First it locates a match of what is supposed to be replaced and second it replaces it with something else.  So for an array you need to consider these two steps.  The first step could be realized using the indexOf() method.  THe replacement aspect could be as simple as reassigning the element found at the index...  array[i] = new thing.

           

          So if you wanted to combine that into one line of code, it would be something to the effect of...

           

               array[array.indexOf(old)] = new;

           

           

          Example:

           

          var array:Array = new Array(2,3,4,5);


          array[array.indexOf(4)] = 8;

           

          trace(array);  // traces 2,3,8,5