Home > InDesign Scripts > Search inside array

Search inside array

With this script we can search inside Array. This is not quite InDesign snippet and can be used in every Javascript. I really like approach to search problem, and want to share it with you. Script is not mine, I just found it on web long time ago.

Array.prototype.exists = function(search){
 for (var i=0; i<this.length; i++)
    if (this[i] == search) return true;
 return false;
}

var myA = Array("yes","no","today","monday");

alert(myA.exists("yes")); // true
alert(myA.exists("maybe")); // false

So, first five lines are function for searching. The prototype property allows you to add properties and methods to an object. Now, when we call ArrayName.exists(“some string”) search function will go through array and try to find match. If the match is function will return true, or if not, then false. We create sample Array called myA, and test it with last two lines.

About these ads
Categories: InDesign Scripts Tags:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 34 other followers

%d bloggers like this: