4 Replies Latest reply on Feb 16, 2012 8:36 AM by NateO12

    declaring var, changing the name

    Jump_Over Level 5

      I have an myArray created from list of master pageItems:



      I am going to use every of this Array element in my dialog as a

      checkboxControls.add ({staticLabel: myArray[k]});


      At the same time I should declare a variables which will keep user checkedState:  .


      So thats should be myChck_1, myChck_2, ... myChck_n,  - depends on myArray.length - declared.


      How can I declare variables with names changing with counter "k" value?



        • 1. Re: declaring var, changing the name
          [Jongware] Most Valuable Participant

          Use an array for the 'names' as well:


          var myChck = new Array;
          for (loop=0; loop<10; loop++)
            myChck[loop] = checkboxControls.add ({staticLabel: myArray[k]});
          1 person found this helpful
          • 2. Re: declaring var, changing the name

            Can this be expanded upon a bit? This is exactly the logic I'm trying to use for my script, but this double array stuff is confusing the heck out of me. How does one loop through the new array for the names while also looping through the checkbox array?

            • 3. Re: declaring var, changing the name
              Marc Autret Level 4

              Let's try to make it clear.


              Given an array of strings—say myNames—the goal is to associate a control (here a checkbox) to each string.


              The first stage is to create each control within the UI container. This can be done easily, as illustrated in the original post:


              // myNames is an array of strings
              var i;
              for( i=0 ; i < myNames.length ; ++i )
                 myCheckboxControls.add({ staticLabel: myNames[i] });


              The above code produces the corresponding control objects—instances of CheckboxControl—but it does not provide a convenient way to access these objects using the original index (in myNames). What the author needs is to keep a reference to each control and to have access to each reference the same way he loops through myNames (i.e., by index). Hence, the ideal structure for this is another array.


              The point is that the CheckboxControls.add(...) method returns a reference to the CheckboxControl object  it just created. You can store this reference in a variable, or directly within your destination array.


              In JavaScript, arrays basically behave as regular objects in that you can fill them 'on the fly' using an integer—the index—in the role of the property name. We can therefore develop the initial code as follows:


              // myNames is an array of strings
              var i;
              var destArray = []; // declare an empty Array
              for( i=0 ; i < myNames.length ; ++i )
                 destArray[i] = myCheckboxControls.add({ staticLabel: myNames[i] });


              The only improvement is that the reference to each newly created control is registered in destArray.


              > How does one loop through the new array for the names while also looping through the checkbox array?


              In fact, the code loops through an existing array (myNames), builds for each index a control based on myNames[i] and stores the corresponding reference in destArray using the same index. The resulting array is gradually filled during the loop. and the correspondence between destArray and myNames is maintained through a common index.


              Now, given a specific index, k, one can easily access to both the corresponding label (myNames[k]) and the corresponding control state: destArray[k].checkedState.




              • 4. Re: declaring var, changing the name
                NateO12 Level 1

                That makes sense Marc, thanks. I'm going to keep this info in a PDF somewhere for future reference.