Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Looping through arrays and assigning values

Participant ,
Jun 13, 2016 Jun 13, 2016

Hello,

I'm using a document level array with 16 elements to hold values, each of these values corresponds to a text field.

What I am wanting to do is create 2 arrays, one storing the values and one storing the field names themselves in the correct order.

eg element 0 of both arrays correspond to the text field and it's stored value.

I want to know if I can loop through both of these arrays and set the values in one sweep rather than writing;

var txt01 = this.getField("Text1");

var txt02 = this.getField("Text2");

//... Etc

txt01.value = arrTextVal[0];

txt02.value = arrTextVal[1];

//... Etc

Could I write something more along the lines of;

for (var i in arrTextVal && arrTextName) {

     arrTextName.value = arrTextVal;

}

I realise this may be completely wrong but I'm trying to give a clear example if what I'm trying to achieve.

Any help is much appreciated.

Thank you in advance.

TOPICS
Acrobat SDK and JavaScript , Windows
2.0K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jun 13, 2016 Jun 13, 2016

Assuming the two arrays have the same length and the items in them are in the same order you can simply do this:

for (var i=0; i<arrTextVal.length; i++) {

    arrTextName.value = arrTextVal;

}

Translate
Community Expert ,
Jun 13, 2016 Jun 13, 2016

Assuming the two arrays have the same length and the items in them are in the same order you can simply do this:

for (var i=0; i<arrTextVal.length; i++) {

    arrTextName.value = arrTextVal;

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jun 13, 2016 Jun 13, 2016

I've got something a little more involved in mind next but I'm not sure if it's even possible.

I want to take that array structure and create it from scratch with another JavaScript, however there is a catch, I would need to do this many times and each Array needs a unique name, am I able to increment an array's name on creation?

The values inside the array would be the same, 16 values and 16 text field names, however the array does need to be unique.

So for example something along the lines of;

var i = 0; i++;

var newArrayVal = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);

This is because as with the majority of the things I am doing this system needs to work with templates and spawned pages and so each newly spawned page needs it's own unique array

Would this even be possible?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 13, 2016 Jun 13, 2016

You can create an array of arrays. You can't change the name of a variable dynamically, though.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jun 13, 2016 Jun 13, 2016

Thank you for the quick reply, I'll have a look into creating an array of arrays.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 13, 2016 Jun 13, 2016

Use something like this:

for (var i=0; i<arrTextVal.length; i++) {  

    this.getField(arrTextName).value = arrTextVal;  

}  

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jun 13, 2016 Jun 13, 2016

Thank you both for your helpful answers!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 13, 2016 Jun 13, 2016
LATEST

You might also want to look using hierarchical field names and the "getArray()" field method.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines