Skip to main content
New Participant
October 11, 2022
Question

Count field in an arrray

  • October 11, 2022
  • 1 reply
  • 786 views

What is the simplest way to count the field in a array of form fields?

I frequent have reason to work with groups of field, such as myField.1, myField.2, myField.3, etc.

I would like to have a simple way to get the number of field in such a group for creating functions to loop through the fields and not have to hard code a specific number fields each time.

this.getField("myField").getArray().length does not work for me.

This topic has been closed for replies.

1 reply

Nesa Nurani
Inspiring
October 11, 2022

If you show it in a field use:

event.value = this.getField("myField").getArray().length;

or set it to variable:

var num = this.getField("myField").getArray().length;

New Participant
October 11, 2022

That works fine as an event.value, but as a variable in a function, it doesn't return anything.

The most verbose iteration I tried is:

function numFields(fieldName){
    var num = 0;
    num = this.getField(fieldName).getArray().length;
    return num;
}
event.value = numFields("myField");

Neither this nor simpler forms return any value.

try67
Braniac
October 11, 2022

Check the JS Console and you'll see an error message regarding your code. Something like this:

TypeError: redeclaration of const numFields
1:Field:Calculate

This is because there's already a built-in property called numFields, so you can't declare a function with that name. Change it to something else and it will work.