5 Replies Latest reply on Mar 31, 2014 9:58 PM by BEGINNER_X

    combine two arrays

    BEGINNER_X Level 3

      Hi All,

       

      1. I want to combine two arrays using for loop.

       

      2. Remove duplication

       

      Combine myParray and myCarray.

       

      //~ var myFinal = myParray.concat(myCarray)

       

      //~ alert("myFinal: " + myFinal)

       

       

      var myDoc = app.activeDocument;
      
      var myPStyles = myDoc.allParagraphStyles;
      
      myParray = new Array();
      
      for(i=0; i<myPStyles.length; i++)
      {
          myParray.push(myPStyles[i])
          }
      alert("myParray: " + myParray)
      
      var myCstyles = myDoc.allCharacterStyles;
      myCarray = new Array();
      
      for(k=0; k<myCstyles.length; k++)
      {
          myCarray.push(myCstyles[k])
          }
      
      alert("myCarray: " + myCarray)
      
      //Combine Two Arrays
      
      for(i=0; i<myParray.length; i++)
      {
          for(k=0; k<myCarray.length; k++)
          {
              myParray[i].push(myCarray[k])
              }
          }
      
      alert("myFinalParray: " + myParray)    //Need output here
      

       

      Thanks

      Beginner_X

        • 1. Re: combine two arrays
          Ronald63 Level 4

          Hi,

           

          Try this ...

          var array1 = ['a','b','c','d'];

          var array2 = ['c','d','e','f'];

          var array3 = array1.concat(array2).unique();

          alert(array3);

           

           

          Array.prototype.unique = function() {

              var a = this.concat();

              for(var i=0; i<a.length; ++i) {

                  for(var j=i+1; j<a.length; ++j) {

                      if(a[i] === a[j])

                          a.splice(j, 1);

                  }

              }

              return a;

          };

          1 person found this helpful
          • 2. Re: combine two arrays
            Trevorׅ Adobe Community Professional

            Hi X

             

            All character / paragraph styles are going to be unique.

            Maybe you are looking for a list of unique names?

            • 3. Re: combine two arrays
              Trevorׅ Adobe Community Professional
              var doc = app.activeDocument,
                      psNames = doc.paragraphStyles.everyItem().name,
                      csNames= doc.characterStyles.everyItem().name,
                      allNames = [],
                      allNames0 = {},
                      z = 0, n, lp = psNames.length, lc = csNames.length;
              while (lp--) allNames0[psNames[lp]] = psNames[lp];
              while (lc--) allNames0[csNames[lc]] = csNames[lc];
              for (n in allNames0) allNames[z++] = allNames0[n];
              allNames.reverse();
              alert (allNames.join("\r"));
              
              • 4. Re: combine two arrays
                Trevorׅ Adobe Community Professional

                Hi Beginner

                 

                Was your question answered?

                • 5. Re: combine two arrays
                  BEGINNER_X Level 3

                  Hi Trevor/Ronald,

                   

                  Apologies for the late response....

                   

                  I was out of city for the last two days.

                   

                  Trevor: Checked your code it works fine in my end.

                   

                  Ronald: Already I got this code in google. Anyway thanks for your help.

                   

                  Coding edited as per my knowledge.

                   

                   

                  var doc = app.activeDocument,
                  
                  psNames = doc.paragraphStyles.everyItem().name,
                  csNames= doc.characterStyles.everyItem().name,
                  pstyles = [];
                  cstyles = [];
                  
                  var z = 0, n;
                  
                  for(lp=0; lp<psNames.length; lp++)
                  {
                      var myPArray = pstyles.push(psNames[lp])
                      }
                  alert("myPArray: " + myPArray)
                  
                  for(lc= 0; lc<csNames.length; lc++)
                  {
                      var myCArray = cstyles.push(csNames[lc])
                      }
                  alert("myCArray: " + myCArray)
                  
                  for (n in cstyles) 
                  {
                      pstyles[z++] = cstyles[n];
                      }
                  alert (pstyles.join("\r"));
                  

                   

                  Thousand Thanks

                  Beginner_X