简体   繁体   English

javascript - 使用 foreach 循环通过表创建数组

[英]javascript - create array with foreach loop through table

i have a form with table like this:我有一个像这样的表格:

<table id="preview">
<tbody>
<tr>
<td>01/01/2010</td>
<td>Credit</td>
<td>1000</td>
</tr>
<tr>
<td>01/05/2010</td>
<td>Debit</td>
<td>200</td>
</tr>
<tr>
<td>01/09/2010</td>
<td>Debit</td>
<td>400</td>
</tr>
<tr>
<td>01/11/2010</td>
<td>Debit</td>
<td>100</td>
</tr>
</tbody>
</table>

Now I need to create an array to send (ajax/php) like this:现在我需要创建一个数组来发送(ajax/php),如下所示:

$ajaxArray = array(   0 => array(From   => "01/01/2010",  //1st td of the 1st Row
                                    To     => "01/05/2010",  //1st td of the 2nd Row
                                    Type   => "Credit",
                                    Amount => 1000.00),
                         1 => array(From   => "01/05/2010",  //1st td of the 2nd Row
                                    To     => "01/09/2010",  //1st td of the 3th Row
                                    Type   => "Debit",
                                    Amount => 200.00),
                         2 => array(From   => "01/09/2010",  //1st td of the 3th Row
                                    To     => "01/11/2010",  //1st td of the 4th Row
                                    Type   => "Debit",
                                    Amount => 400.00),
                         3 => array(From   => "01/11/2010",  //1st td of the 4th Row
                                    To     => "01/01/2012",  //Value in $last_date var
                                    Type   => "Debit",
                                    Amount => 100.00)
        );

I tried with this code:我尝试使用此代码:

 $('#preview > tbody  > tr').each(function() {
            var from = $('td:eq(0) ', this).text();
            var type = $('td:eq(1) ', this).text();
            var amount = $('td:eq(2) ', this).text();
            ajaxArray.push({
                From: from,
                Type: type,
                Amount: amount
            });
        });

As you can see i can't get the "To" date value.如您所见,我无法获得“To”日期值。 The "To" date value is the date contained in the 1st TD of the Next row except for the last Row, where this value is in the $last_date variable. “到”日期值是包含在下一行的第一个 TD 中的日期,最后一行除外,该值在 $last_date 变量中。

Thanks in advance提前致谢

This should do it Fiddle Demo这应该做到Fiddle Demo

 var array = []; var rows = $("#preview tbody tr"); $.each( rows, function(index, row) { var columns = $(row).find("td"); array[index] = {}; array[index].from = columns[0].innerHTML; array[index].type = columns[1].innerHTML; array[index].amount = columns[2].innerHTML; if(index > 0){ array[index - 1].to = columns[0].innerHTML; } }); $("#result").text(JSON.stringify(array));
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id="preview"> <tbody> <tr> <td>01/01/2010</td> <td>Credit</td> <td>1000</td> </tr> <tr> <td>01/05/2010</td> <td>Debit</td> <td>200</td> </tr> <tr> <td>01/09/2010</td> <td>Debit</td> <td>400</td> </tr> <tr> <td>01/11/2010</td> <td>Debit</td> <td>100</td> </tr> </tbody> </table> <hr/> <pre id="result"></pre>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM