3 Replies Latest reply on Feb 10, 2016 3:11 PM by Loic.Aigon

    How do I condense my (array) script?


      My script is as follows: (it is functional, just wondering if I could shorten it)


           words[0].applyCharacterStyle(csHeaderMedium, true);
           words[1].applyCharacterStyle(csHeaderSmall, true);
           words[2].applyCharacterStyle(csHeaderMedium, true);
           words[3].applyCharacterStyle(csHeaderSmall, true);
           words[4].applyCharacterStyle(csHeaderMedium, true);
           words[7].applyCharacterStyle(csHeaderMedium, true);
           words[8].applyCharacterStyle(csHeaderSmall, true);


      Any help would be appreciated

        • 1. Re: How do I condense my (array) script?
          Loic.Aigon Adobe Community Professional



          Your problem here is there isn't any consistency between values. Nor the indeces are contigous, nor the applied style can be set from the index value (i.e. odd or even for ex.). So you can end with a function but not sure how much it would save efforts and code redundancy…


          A proposal :


          var fn = function(index, style){
            tfRHeader.words[index].applyCharacterStyle(style, true);
          fn(0, csHeaderMedium);
          fn(1, csHeaderSmall);
          fn(2, csHeaderMedium);
          fn(3, csHeaderSmall);
          fn(4, csHeaderMedium);
          fn(7, csHeaderMedium);
          fn(8, csHeaderSmall);





          Loïc Aigon


          • 2. Re: How do I condense my (array) script?
            Schmaltzkopf Level 1

            my question is: Is there a way to target more then one index in an array at time, like array[1,3] (this did not work in my experiment - it only targeted 3, not 1)

            Am I missing something?

            • 3. Re: How do I condense my (array) script?
              Loic.Aigon Adobe Community Professional
              var fn = function(indexArray, style){
                var i;
                if ( !indexArray
                || !(indexArray instanceof Array)
                || !indexArray.length 
                || !style
                || !(style instanceof CharacterStyle)
                || !style.isValid) return;
                while (i = indexArray.pop() ) {
                !isNaN(i) && tfRHeader.words[i].applyCharacterStyle(style, true); 
              fn([0,2,4,7], csHeaderMedium);
              fn([1,3,8], csHeaderSmall);