Quantcast
Channel: Adobe Community: Message List - InDesign Scripting
Viewing all articles
Browse latest Browse all 37788

[JS][CC] checking an Array using IndexOf

$
0
0

Hi.

 

I have no idea if this will help others, or if I have over complicated the issue, but wanted to share a recent issue I had.

 

I was writing a script and wanted to check the contents of an array, and add the value if it was not already in the Array using IndexOf as below:

(I was buidling up an array of used paragraph styles, as it happens)

 

function pushUnique(arrayName,valueToCheck){

          var myIndex = arrayName.indexOf(valueToCheck);

          if(myIndex==-1){

                    arrayName.push(valueToCheck);

                    }

          }

 

 

However, I was unable to use IndexOf on the array.

 

Adding the following code at the top of my Library file, added the IndexOf prototype to the Array object, and made it work fine!

 

 

 

Array.prototype.indexOf = function ( item ) {

          var index = 0, length = this.length;

          for ( ; index < length; index++ ) {

                    if ( this[index] === item )

                              return index;

          }

          return -1;

          };

 

 

Any thoughts on this would be good, is this good/bad practice to be messing with the Objects prototypes?

 

This tip is from a Javascript Developer who is very knowledgeable on Javascript, but never used the InDesign DOM. (my Son)


Viewing all articles
Browse latest Browse all 37788

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>