How to compare a variable to array
Copy link to clipboard
Copied
How do you compare a variable (x) to an array to determine if the value is inlcuded? This would then be used to toggle a feild boarder color (true = black, false = red).
Ex: var X = "K"
var array1 = new Array("A", "B", "C", "D", "E", "F", "G", "H", "J", "K")
Copy link to clipboard
Copied
What exactly are you trying to compare?
Are you asking how to compare the value of a text field against a the value of another text field?
Are the letters in your array actual field names?
Copy link to clipboard
Copied
Var X is deteremined by a charater search in a text string (e.g. charAt(4)), which then is used to verify if the X value (K in my example) is in a list of options (A thru K in my example) to ensure that it is a valid parameter of the part number. I can accomplish this with an if/else statement, but am hoping for something more effiecent.
@JR Boulay - Thank you for the link, that is what sent me down this road. I'm just not understanding how to apply it in this instance based on the two examples.
Copy link to clipboard
Copied
You can implement it like this:
var X = "K"
var array1 = new Array("A", "B", "C", "D", "E", "F", "G", "H", "J", "K")
if (array1.indexOf(X)==-1) {
app.alert("Value NOT found in array.",3);
} else {
app.alert("Value FOUND in array.",3);
}
Copy link to clipboard
Copied
Ah, that I can follow. Thanks!
What does the ", 3)" do in the aap.alert funtion? I've seen ",0)" in examples also.
Copy link to clipboard
Copied
It determines the icon that appears in the dialog window. 3 is the blue info icon. 0 is the red error cross.
Copy link to clipboard
Copied
Got it. Thanks again.
Copy link to clipboard
Copied
Try this:
array1.indexOf(X)
Copy link to clipboard
Copied
Might be good to mention that this method returns a number, not a boolean. It will either return the index of the value in the array (zero-based, of course), or -1, if it's not present.
Copy link to clipboard
Copied
You should use the find() method:
https://www.w3schools.com/jsref/jsref_find.asp
Copy link to clipboard
Copied
It will not work in older versions of Acrobat and Reader, though, unlike indexOf.
Copy link to clipboard
Copied
Using Prod DC 2022.001.20117
Copy link to clipboard
Copied
But if the file is going to be used by others you have to take it into account that they might be using an older version of Reader or Acrobat...
Copy link to clipboard
Copied
Does find() work in Acrobat? I input the two scripts below, and get "TypeError: [array].find is not a function"
Script from link provided:
var ages = [3, 10, 18, 20];
function checkAge(age) {
return age > 18;
}
this.getField("Test").value = ages.find(checkAge);
Script from another website:
var array1 = [5, 12, 8, 130, 44];
var found = array1.find(element => element > 5 && element <44);
console.println(found);
Copy link to clipboard
Copied
You can use indexOf() to check an array for value, it will return the position of an item in the array at which the value is found, if it's not found it will return -1.
Copy link to clipboard
Copied
Thank you Nesa. So I would have to find the index, and then call that array referencing the index number to get that index's value?
Copy link to clipboard
Copied
Disregard, that's pointless with an array of values. I was planing on using find() with an array of objects, so the sought value of the object (ex. obj.color) would be returned based on the variable in the find() funciton (ex. obj.temp).
Copy link to clipboard
Copied
Be aware that indexOf only works with literal values, not with conditions. So there's no point accessing the value once you know its index. You already know what it is.
Copy link to clipboard
Copied
You can use a loop over the array.
Copy link to clipboard
Copied
Thanks all.

