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

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

2.1K

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
Community Expert ,
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?

Votes

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.

Votes

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
Community Expert ,
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);
}

Votes

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.

Votes

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
Community Expert ,
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.

Votes

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

Got it.  Thanks again.

Votes

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

Copy link to clipboard

Copied

Try this:

array1.indexOf(X)

 

Votes

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
Community Expert ,
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.

Votes

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
Community Expert ,
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

Votes

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
Community Expert ,
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.

Votes

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

Votes

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
Community Expert ,
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...

Votes

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

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

Votes

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

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.

Votes

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

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?

Votes

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

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

Votes

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

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.

Votes

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

Copy link to clipboard

Copied

You can use a loop over the array. 

Votes

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

Copy link to clipboard

Copied

LATEST

Thanks all.

Votes

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