Declare an empty two-dimensional array in Javascript?

后端 未结 18 1377
暖寄归人
暖寄归人 2020-11-29 21:45

I want to create a two dimensional array in Javascript where I\'m going to store coordinates (x,y). I don\'t know yet how many pairs of coordinates I will have because they

18条回答
  •  萌比男神i
    2020-11-29 22:25

    We usually know the number of columns but maybe not rows (records). Here is an example of my solution making use of much of the above here. (For those here more experienced in JS than me - pretty much everone - any code improvement suggestions welcome)

         var a_cols = [null,null,null,null,null,null,null,null,null];
         var a_rxc  = [[a_cols]];
    
         // just checking  var arr =  a_rxc.length ; //Array.isArray(a_rxc);
         // alert ("a_rxc length=" + arr) ; Returned 1 
         /* Quick test of array to check can assign new rows to a_rxc. 
            i can be treated as the rows dimension and  j the columns*/
           for (i=0; i<3; i++) {
              for (j=0; j<9; j++) {
                a_rxc[i][j] = i*j;
                alert ("i=" + i + "j=" + j + "  "  + a_rxc[i][j] );
               }
              if (i+1<3) { a_rxc[i+1] = [[a_cols]]; }
            }
    

    And if passing this array to the sever the ajax that works for me is

     $.post("../ajax/myservercode.php",
           {
            jqArrArg1 : a_onedimarray,
            jqArrArg2 : a_rxc
           },
           function(){  },"text" )
            .done(function(srvresp,status) { $("#id_PageContainer").html(srvresp);} ) 
            .fail(function(jqXHR,status) { alert("jqXHR AJAX error " + jqXHR + ">>" + status );} );
    

提交回复
热议问题