Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to compare a variable to array

New Here ,
Jun 03, 2022 Jun 03, 2022

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
3.1K
Translate
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
Community Expert ,
Jun 03, 2022 Jun 03, 2022

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?

Translate
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

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.

Translate
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
Community Expert ,
Jun 06, 2022 Jun 06, 2022

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

Ah, that I can follow.  Thanks!

 

What does the ", 3)" do in the aap.alert funtion?  I've seen ",0)" in examples also.

Translate
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
Community Expert ,
Jun 07, 2022 Jun 07, 2022

It determines the icon that appears in the dialog window. 3 is the blue info icon. 0 is the red error cross.

Translate
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

Got it.  Thanks again.

Translate
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
Community Expert ,
Jun 03, 2022 Jun 03, 2022

Try this:

array1.indexOf(X)

 

Translate
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
Community Expert ,
Jun 04, 2022 Jun 04, 2022

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.

Translate
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
Community Expert ,
Jun 04, 2022 Jun 04, 2022

You should use the find() method:

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

PDF Acrobatic, InDesigner & Photoshoptographer
Translate
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
Community Expert ,
Jun 04, 2022 Jun 04, 2022

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

Translate
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

Using Prod DC 2022.001.20117

Translate
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
Community Expert ,
Jun 06, 2022 Jun 06, 2022

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

Translate
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 ,
Mar 27, 2023 Mar 27, 2023

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

Translate
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
Community Expert ,
Mar 27, 2023 Mar 27, 2023

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.

Translate
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 ,
Mar 27, 2023 Mar 27, 2023

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?

Translate
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 ,
Mar 27, 2023 Mar 27, 2023

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

Translate
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
Community Expert ,
Mar 27, 2023 Mar 27, 2023

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.

Translate
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
Community Expert ,
Mar 27, 2023 Mar 27, 2023

You can use a loop over the array. 

Translate
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 ,
Mar 27, 2023 Mar 27, 2023
LATEST

Thanks all.

Translate
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