www.webdeveloper.com
Results 1 to 3 of 3

Thread: Array of Arrays creation question

Hybrid View

  1. #1
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,434

    Question Array of Arrays creation question

    In the following script, the 'choices' array is defined correctly.
    It also displays as expected with the showArray() function.

    I was experimenting to see if I could dynamically create a similiar array of array (picks),
    but I'm having difficulty with my syntax.

    I highlighted the commands I believe are not correct in RED.
    Does anyone have an idea how I can duplicate the 'choices' array into the 'pick' array.

    Code:
    <html>
    <body>
    <form id="drop_list" onsubmit="return false">
     <button onclick="showArray(choices)">Choice display</button>
     <button onclick="showPicks()">Picks display</button>
    </form>
    
    <script type="text/javascript">
        var choices = [  // an array OF arrays */
            [ "AK", "Alaska" ],
            [ "AL", "Alabama" ],
            [ "AR", "Arkansas" ],
            [ "AZ", "Arizona" ],
            [ "CA", "California" ],
            [ "FL", "Florida" ],
            [ "GA", "Georgia" ],
            [ "TX", "Texas" ],
            [ "VT", "Vermont" ]  // Note: No comma after last entry
        ];
    
    // want to create above array of arrays from strings below
      var picks = [];
      var arrStates = [
          "AK:Alaska",      "AL:Alabama",      "AR:Arkansas",    "AZ:Arizona",
          "CA:California",  "FL:Florida",      "GA:Georgia",     "TX:Texas",      "VT:Vermont" ];
    
    // following is NOT used at this time.
    //  var strStates = "AK:Alaska|AL:Alabama|AR:Arkansas|AZ:Arizona|CA:California|FL:Florida|GA:Georgia|TX:Texas|VT:Vermont";
      
    function showPicks() {
      var tarr = [];
      for (var i=0; i<arrStates.length; i++) {
        tarr = arrStates[i].split(':');
        picks.push([tarr[0]],[tarr[1]]);        // this does not appear to work as desired
    //    picks.push([tarr[0]]+','+[tarr[1]]);  // neither does this
      }
      showArray(picks);
    }
    function showArray(arr) {
      var abb = sta = '';
      for (var i=0; i<arr.length; i++) { abb += arr[i][0]+'\n'; }
      for (var i=0; i<arr.length; i++) { sta += arr[i][1]+'\n'; }
      alert(arr.join('\n')+'\n\n'+abb+'\n\n'+sta);
    }
    </script>
    </body>
    </html>

  2. #2
    Join Date
    Jan 2010
    Posts
    80
    Hi bud
    You seem to have an extra set of square brackets in your push which is adding two elements to your array rather than the array if you change it to
    Code:
        picks.push([tarr[0],tarr[1]]);
    You could simplify a little more by doing
    Code:
          picks.push(arrStates[i].split(':'));
    This means that you could get rid of your temporary “tar” array.

    Hope this helps

    V

  3. #3
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,434
    Thank you very much.
    That was the syntax I was looking for.

    I may have been close with my attempt,
    but close only counts in horseshoes and handgranades.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles