Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jul 2013
    Posts
    48
    Thanks
    9
    Thanked 1 Time in 1 Post

    How to put arrays values in table

    hi to all. I have 4 arrays :

    [0, 15, 0, 1]
    [35, 21, 7, 0]
    [15, 20, 5, 0]
    [0, 11, 0, 0]

    With names params, cols, n_c, n_p
    Can you tell me how to put them in a table.

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,966
    Thanks
    0
    Thanked 236 Times in 233 Posts
    This?
    Code:
    <table id="myTable"></table>
    <script type="text/javascript">
    var params = [0, 15, 0, 1],
        cols =[35, 21, 7, 0],
        n_c = [15, 20, 5, 0],
        n_p = [0, 11, 0, 0],
        tbl = [params, cols, n_c, n_p],
        html = '',
        i, j, row;
    
    for (i=0; i<tbl.length; i++) {
        row = tbl[i];
        html += '<tr>';
        for (j=0; j<row.length; j++) {
            html += '<td>' + row[j] + '</td>';
        }
        html += '</tr>';
    }
    
    document.getElementById('myTable').innerHTML = html;
    </script>

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Hmmm...I think it would be more meaningful to use
    Code:
    var tbl = {
        params : [0, 15, 0, 1],
        cols : [35, 21, 7, 0],
        n_c : [15, 20, 5, 0],
        n_p : [0, 11, 0, 0]
    };
    And then you can refer to elements as, for example only, tbl.cols[2] and tbl.n_c[1].

    Much clearer than having to do tbl[1][2] and tbl[2][1], no?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,966
    Thanks
    0
    Thanked 236 Times in 233 Posts
    You're right.


  •  

    Posting Permissions

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