irenem
January 21, 2008, 9:39am
1
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
irenem
January 21, 2008, 12:22pm
3
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
patrick
January 21, 2008, 12:53pm
4
Some more thoughts:
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
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…
irenem
January 21, 2008, 1:05pm
5
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”.
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