Skip to main content
Inspiring
January 24, 2020
Answered

PDF JS: use part of list as variable

  • January 24, 2020
  • 1 reply
  • 660 views

Hey everyone, 

 

I have a list 

x = ['Foo_1','Bar_2']

 

Foo is the name of a field in the same form for which I need the value - I then want to check if that value is equal or lower than 1 and then store that result (as ill need to do the same for multiple items in the list)

 

Using the fact that the fieldname will always be three characters long I've written x.substr(0,3)  which I then use to get the field value

if (this.getField(x[0].substr(0,3)).value <= x[0].substr(-1)){
                //store the fact that the Foo field is lower or equal to one (in this example) 
            }
 

So my question is can I use the string resulting from  x[0].substr(0,3) in the name for a variable? 

 

when I try:

var 'state_'+x[0].substr(0,3) = 'Yep'

I get the error 'missing variable name'

 

Any suggestions how I can solve this?

 

Thank you in advance for any trouble to be taken

This topic has been closed for replies.
Correct answer Bernd Alheit

You can use a associative array:

 

var arr = [];
arr['state_'+x[0].substr(0,3)] = 'Yep';

1 reply

Bernd Alheit
Community Expert
Bernd AlheitCommunity ExpertCorrect answer
Community Expert
January 24, 2020

You can use a associative array:

 

var arr = [];
arr['state_'+x[0].substr(0,3)] = 'Yep';

Inspiring
January 24, 2020

Ah very cool, yeah that works

thanks a ton bernd!