PDF JS: use part of list as variable

Participant ,
Jan 24, 2020

Copy link to clipboard

Copied

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

Adobe Community Professional
Correct answer by Bernd Alheit | Adobe Community Professional

You can use a associative array:

 

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

TOPICS
Acrobat SDK and JavaScript, How to, PDF forms

Views

56

Likes

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

PDF JS: use part of list as variable

Participant ,
Jan 24, 2020

Copy link to clipboard

Copied

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

Adobe Community Professional
Correct answer by Bernd Alheit | Adobe Community Professional

You can use a associative array:

 

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

TOPICS
Acrobat SDK and JavaScript, How to, PDF forms

Views

57

Likes

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
Jan 24, 2020 0
Adobe Community Professional ,
Jan 24, 2020

Copy link to clipboard

Copied

You can use a associative array:

 

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

Likes

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
Reply
Loading...
Jan 24, 2020 1
Participant ,
Jan 24, 2020

Copy link to clipboard

Copied

Ah very cool, yeah that works

thanks a ton bernd! 

Likes

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
Reply
Loading...
Jan 24, 2020 0