How to compare a variable to array

New Here ,
Jun 03, 2022 Jun 03, 2022

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")

TOPICS
How to , JavaScript , PDF forms

Views

109

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jun 03, 2022 Jun 03, 2022

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?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 06, 2022 Jun 06, 2022

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.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jun 06, 2022 Jun 06, 2022

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);
}

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 06, 2022 Jun 06, 2022

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.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jun 07, 2022 Jun 07, 2022

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.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 07, 2022 Jun 07, 2022

Copy link to clipboard

Copied

LATEST

Got it.  Thanks again.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jun 03, 2022 Jun 03, 2022

Copy link to clipboard

Copied

Try this:

array1.indexOf(X)

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jun 04, 2022 Jun 04, 2022

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.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jun 04, 2022 Jun 04, 2022

Copy link to clipboard

Copied

You should use the find() method:

https://www.w3schools.com/jsref/jsref_find.asp

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jun 04, 2022 Jun 04, 2022

Copy link to clipboard

Copied

It will not work in older versions of Acrobat and Reader, though, unlike indexOf.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 06, 2022 Jun 06, 2022

Copy link to clipboard

Copied

Using Prod DC 2022.001.20117

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jun 06, 2022 Jun 06, 2022

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...

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines