2 Replies Latest reply on Mar 26, 2017 7:59 AM by Laubender

    Code to be evaluated! [017] Good practices! …

    Obi-wan Kenobi Adobe Community Professional

      Hi Scripters!

       

      Dubitative! 

       

      Good or bad writing!

       

       

      var A = ["Blue","Red","Green","White","Black"];
      A = A.toString().replace(/,Red/,"").split(",");
      $.writeln(A);
      

       

      Capture d’écran 2017-03-25 à 01.14.38.png

      or

       

      var A = ["Blue","Red","Green","White","Black"];
      for ( var a = 0; a < A.length-1 ; a++ ) if ( A[a] == "Red" ) A.splice(a,1);
      $.writeln(A);
      

       

      Capture d’écran 2017-03-25 à 01.15.09.png

       

      Thanks for your comments! 

       

      (^/)

        • 1. Re: Code to be evaluated! [017] Good practices! …
          pixxxel schubser Level 5

          Hi Obi-wan Kenobi,

          what do you really want to do?

          Remove one part of an array by name?

           

          You know ExtendScript does not allow a "getByName in array". That's why: There is no good or bad writing – it is always a case distinction. If you use the "change-array-to-string-and-back-method" you don't need a loop and you can use replace and other "regex methods". This seems to be a small advantage.

           

          But be sure – IMHO it is better to use the global "g-parameter" in the replace method too. And I think leave the array A as it is and create a new Array B with all the changings. And I prefer array.join() instead of array.toString(). This method is more flexible for me.

          var A = ["Blue","Red","Green","White","Black","Red"];
          var B = A.join().replace(/,Red/g,"").split(",");
          $.writeln(B);
          

           

          Have fun

           

          • 2. Re: Code to be evaluated! [017] Good practices! …
            Laubender Adobe Community Professional & MVP

            Hi Obi-wan,

             

            also notice, that pixxxel schubser is using the g parameter for his regular expression.

            It wasn't clear from the start, if you want to remove the first instance of the string or all instances (if there are any).
            Or if that type of array should only contain unique strings…

             

            If all strings are unique when starting with array A, a loop could be terminated with break after the string is found and A.splice(a,1) is done. That would reduce the amount of time running the loop, if the string is not the last entry in the array.

             

            Regards,

            Uwe