1

私はJavaScriptの初心者なので、これに対する解決策は私が思っているよりも基本的かもしれません。このテーブルからデータを取得して配列で表示しようとしています。表は次のように始まります。

<table id="myTable">
     <col>
        <thead>
             <tr>
                <th>Date</th>
                <th>Results1</th>
                <th>Results2</th>
            </tr>
         </thead>
         <tbody>
             <tr>
                <td>Jan-00</td>
                <td>9</td>
                    <td>10</td>
             </tr>
             <tr>
                <td>Feb-00</td>
                <td>92</td>
                <td>64</td>
             </tr>

そしてそこから続きます(それはたくさんの<tr>'を含む長いテーブルです)。

変数を取得するための私のコードは、次のように始まります(これを支援するための他のソースへのヒント):

var columns = $('#myTable thead th').map(function() {
        return $(this).text();
    });
    var tableObject = $('#myTable tr').map(function(i) {
        var row = {};
        // Find all of the table cells on this row.
        $(this).find('td').each(function(i) {
        // Determine the cell's column name by comparing its index
        //  within the row with the columns list we built previously.
        var rowName = columns[i];
        // Add a new property to the row object, using this cell's
        //  column name as the key and the cell's text as the value.
            row[rowName] = $(this).text();
        });
        // Return the row's object representation, to be included
        //  in the array that $.map() ultimately returns.
        return row;
        // .get() to convert the jQuery set to a regular array.
        }).get();
        for (var i in tableObject) {
            $.each(tableObject[i], function(key,value) { 
                var line1 = {};
                line1[key] = value;
                console.log(line1); //test
            });
        }

console.logテストでは、変数line1は、保持キーと値を次のように表示します:日付:Jan 00、Results1:9、Results2:10。

それはすべてうまくいっていますが、私は本当にこれらの値を次の形式で新しい配列に入れる必要があります。

var new = [[Date, Results1], [Date, Results2], etc...];

質問を繰り返しているのに、探していたものがまったく見つからなかった場合は、お詫び申し上げます。誰もが提供できるどんな助けにも本当に感謝します。

4

2 に答える 2

2

これはそれを行う必要があります:

var ary = [];
$('tr').each(function() {
    date = $('td:first', this).text();
    $('td:gt(0)', this).each(function() {
        ary.push([date, $(this).text()]);
    });
});
于 2012-05-25T16:45:31.440 に答える
0

http://www.w3schools.com/jsref/dom_obj_table.aspを見てください。tableObject .rows を 使用して、セルを含む行の配列を取得することをお勧めします。次に、.cells を使用してすべてのセル要素を取得します。それは次のように見えるかもしれません

   var table =document.getElementById("myTable")
  var foo = new array(table.rows.length);
  for(var j =0; j<foo.length; j++)
   foo[j]=foo[j].cells;

これにより、テーブルが foo という 2 次元配列になります。これは、foo[rowindex][col/cellindex] の形式で表されます (つまり、foo[0][1] は結果 1 を返します。

これで始められることを願っています

于 2012-05-25T19:58:09.193 に答える