4 Replies Latest reply on Nov 13, 2014 11:42 PM by BEGINNER_X

    Sharing Knowledge

    BEGINNER_X Level 3

      Hi All,

       

      Use below function for avoid Unique or duplicate in arrays:

       

      myTextFile2.write( "Ligatures Missing\t" + unique(myParaPageArray) + "\n")
      
      function unique(myPs) {
          var newArray = new Array();
          label: for (var i = 0; i < myPs.length; i++) {
              for (var j = 0; j < newArray.length; j++) {
                  if (newArray[j] == myPs[i]) continue label;
              }
              newArray[newArray.length] = myPs[i];
          }
          return newArray;
      }
      
      

       

      Please share this kind of functions. Surely helpful for lot of programmers.

       

      Regards

      Siraj

        • 1. Re: Sharing Knowledge
          Loic.Aigon Adobe Community Professional

          Hi Siraj,

           

          I guess the most valuable place for sharing such snippets is GitHub. I have dropped an eye and there are already various js array libraries. If not, you can start creating one and have fellows enriching it

           

          Loic

          • 2. Re: Re: Sharing Knowledge
            BEGINNER_X Level 3

            Hi Loic,

             

            Thanks for your knowledge...

             

            I learn how to use "GitHub" and get back to you...

             

            Any other function is appreciable....

             

            thanks Chinna,

            Another Function:

             

            Array.prototype.contains = function(k)   
            {  
              for(var i=0; i < this.length; i++)   
              {  
                    if(this[i] === k)   
                    {  
                        return true;   
                    }  
                }  
              return false;   
            }  
            var _layers = [ "BaseArtwork", "MagentaVariables", "PreprintedText", "OtherVariables"];   
            var match = [];   
            var ly = app.activeDocument.layers.everyItem().getElements();   
            if(ly.length == 4){  
                for (i=0; i<ly.length; i++)   
                {  
                    if (_layers.contains(ly[i].name))   
                    {  
                            match.push(i);  
                        }  
                } 
            } 
            
            
            • 3. Re: Re: Sharing Knowledge
              Sajeev Sridharan Level 4

              Nice Sharing...

               

              Another method:

               

              var myTestArray = [10, 10, 12, 12, 14, 15];
              alert( "Unique Array List\t" + unique(myTestArray)); 
              function unique(myArray)
              {
                  var test=[];
                  var newArray = [];
                  for (i=0;i<myArray.length;i++)
                  {
                      test[myArray[i]]=0;
                  }
                 for (i in test)
                  {
                      newArray.push(i);
                  }
                  return newArray;
              }
              

               

               

              Vandy

              • 4. Re: Re: Re: Sharing Knowledge
                BEGINNER_X Level 3

                Hi All,

                My plan is gather this kind of functions and how to use it like for beginner and junior developer.

                 

                Already Mentioned function but with different examples...

                 

                Thanks csm_phil, this is your coding

                 

                var myFile = File.openDialog("Choose a tab delimitted txt file:");  
                if (!myFile){  
                exit();  
                }       
                  
                var myArray = []   
                myFile.open('r');  
                while (myFile.eof==false){  
                    var line=myFile.readln().split(",");  
                    myArray.push(line[0]);  
                    }  
                  
                var myDoc = app.activeDocument;  
                var myPstyles = app.activeDocument.allParagraphStyles;  
                  
                Array.prototype.contains = function(obj) {  
                    var i = this.length;  
                    while (i--) {  
                        if (this[i] === obj) {  
                            return true;  
                        }  
                    }  
                    return false;  
                }  
                  
                var myMissingStyle =[];  
                for(i=2; i<myPstyles.length; i++){  
                    var myS =myPstyles[i].name;  
                    if(myArray.contains(myS) !=true){  
                        myMissingStyle.push(myS);  
                        }  
                    }  
                  
                //I showed only missing styles names, If you want a report file, please you have generate it.  
                alert(myMissingStyle.join("\n"));  
                
                

                Regards

                Siraj