Skip to main content
JaySprout
Inspiring
July 7, 2016
Answered

Why is loop skipping fields?

  • July 7, 2016
  • 1 reply
  • 997 views

PDF has multiple pages spawned from templates. Purpose of my function is to find all fields with a given prefix and remove those fields to visually clean up final document and reduce file size. For some reason, the function has to be called multiple times to find and destroy all the fields even though they all meet the same criteria. Here's the function:

// "theSecond" is the target prefix
function selfDestruct(theSecond) {

    // Create empty variable for target field
    var myFullFieldName;

// Finds the number of fields in the PDF and loops through them
for (var i = 0; i < this.numFields; i++) {
 
  // Gets the name of the current field
  var fieldInSight = this.getNthFieldName(i);

  // Splits the field name into sections and puts them in an array
  var fieldNamePieces = fieldInSight.split(".");

  // Checks if field is a target
  if (fieldNamePieces[2] == theSecond) {
  
   // If true, it reassembles the field name
   myFullFieldName = fieldNamePieces[0] + "." + fieldNamePieces[1] + "." + fieldNamePieces[2] + "." + fieldNamePieces[3];
   // show me what's being removed
   console.println(myFullFieldName);
   //
   this.removeField(myFullFieldName);
  
  } // end of if
} // end of for
} // end of function

My argument is either "single", "double", and so on to find and remove the following (the first prefix varies, of course), for example:

  • P2.actions.single.LeftColumn
  • P2.actions.single.RightColumn
  • P2.actions.single.resetTextBox
  • P2.actions.single.TopRow
  • P2.actions.single.BottomRow

Every time I try it (either in the console or a button executing the script), if there's a group of five (as above), it removes three the first time, a fourth the second time, and the fifth/last field on the third try. Consistently and in the same order each time. I have one "group" with only two fields sharing the same prefix and, each time, it deletes one, then the second one the second time I call the function. I can't figure out why it does this or how it determines the order. If there are two or twenty pages with these fields, it always happens the same way in the same order.

It gets weirder: If I spawn new pages between calling the function, it leap-frogs ... getting the "fourth" field it missed last time and the first three on the newly spawned pages.

Please enlighten me. Thanks in advance.

This topic has been closed for replies.
Correct answer try67

It's an "implicit" array, yes, and you should process it from numFields-1 down to 0.

1 reply

try67
Community Expert
Community Expert
July 7, 2016

When you remove items from an array in a loop it's always a good idea to process it from the end to the beginning, so you won't end up with incorrect index values.

JaySprout
JaySproutAuthor
Inspiring
July 7, 2016

Am I understanding you correctly that all document's fields are stored in an array? The only array I thought I was working with in my function was the one created by "split." Are you saying I should run my for loop decrementally instead of incrementally?

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
July 7, 2016

It's an "implicit" array, yes, and you should process it from numFields-1 down to 0.