Match an element in a array

I wonder if I could compare a variable containing an element with another variable containing an array without loop.

Something like

var myArray = [blue, red, green]
var myElement = red

if( myArray contains myElement)
{
beep
}

I’m grateful with any help :?

Hi Irene,

you could convert the array into a string and test if there is a substring in there which matches ?

Something like:

var myArray = ["blue", "red", "green"] ;
var array_as_string = myArray.join ( ";") ;
var myElement = "red" ; 

if( array_as_string.indexOf ( myElement , 1 )) //boolean result 
{ 
beep 
}

Cheers
Harry

Hi Harry,

your suggestion didn’t work for 100% but it helped me on the way.

If myElementsis filled with data that is not present in myArray he returned te bolean as true.

I solved it with patternCount

var myArray = ["blue", "red", "green"] ; 
var array_as_string = myArray.join ( ";") ; 
var myElement = "red" ; 
var myCount =  utils.stringPatternCount(array_as_string,myElement)

if( myCount > 0) 
{ 
beep 
}

Now it works correctly, thank you.

Bye
Irene

Some more thoughts:

  1. I think there is nothing wrong with a loop. You could create a global method that checks this kind of thing and let that method return true or false
  2. Your code will go wrong in some cases. For example
var myArray = ["navy blue", "dark blue"]
var myElement = "blue";

if you want the exact match for blue, you will fail here…

  1. I think there is nothing wrong with a loop. You could create a global method that checks this kind of thing and let that method return true or false

you are right, probably I stick a little bit to much with FileMaker, where loops are a “never ending story”.

  1. Your code will go wrong in some cases. For example

In my case I think it will be ok, myArray and myElements contains email addresses. But you have a good point. I think that I will change my code… just in case :D