Skip to main content
JMFreeman
Inspiring
January 15, 2020
Answered

Help With For Loop, Field Names

  • January 15, 2020
  • 1 reply
  • 845 views

This is a stub from a project. My goal is to look through all the fields in the document, then put any of them whose name begins with "topPh" (they all end in numbers) into my array fields[]. I'm still researching how to decide which names conatin the substring for the if statement, but in the meantime I am testing using this:

     var fields = [];
     for (var i = 0; i < this.numFields; i++){
          var f = this.getField(this.getNthFieldName(i)).name;
          if(f = "topPh2") fields.push(f);
     }     //end of for loop
     app.alert(fields);

I expected it to put one value in fields[]: topPh2. It seems to be doing it many times. In fact, I crashed Acrobat through console doing something similar. This is what it puts out:

 

What am I doing wrong here?

This topic has been closed for replies.
Correct answer JMFreeman

*Sigh*

This was easy. '=' is not a logical operator.... I needed '==':

     var fields = [];
     for (var i = 0; i < this.numFields; i++){
          var f = this.getField(this.getNthFieldName(i)).name;
          if(f == "topPh2") fields.push(f);
     }     //end of for loop
     app.alert(fields);

1 reply

JMFreeman
JMFreemanAuthorCorrect answer
Inspiring
January 15, 2020

*Sigh*

This was easy. '=' is not a logical operator.... I needed '==':

     var fields = [];
     for (var i = 0; i < this.numFields; i++){
          var f = this.getField(this.getNthFieldName(i)).name;
          if(f == "topPh2") fields.push(f);
     }     //end of for loop
     app.alert(fields);