javascript-通过表创建带有foreach循环的数组

javascript-通过表创建带有foreach循环的数组,javascript,jquery,arrays,foreach,Javascript,Jquery,Arrays,Foreach,我有一个表格,表格如下: <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<

我有一个表格,表格如下:

<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>
$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)
        );
我尝试使用以下代码:

 $('#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
            });
        });
如您所见,我无法获取“To”日期值。“到”日期值是下一行的第一个TD中包含的日期,最后一行除外,其中该值位于$last_date变量中

提前感谢

这应该可以

var数组=[];
变量行=$(“#预览tbody tr”);
$.each(行,函数(索引,行){
var columns=$(行).find(“td”);
数组[索引]={};
数组[index]。from=列[0]。innerHTML;
数组[index]。类型=列[1]。innerHTML;
数组[index]。金额=列[2]。innerHTML;
如果(索引>0){
数组[index-1]。收件人=列[0]。innerHTML;
}      
});
$(“#结果”).text(JSON.stringify(数组))

01/01/2010
信用
1000
01/05/2010
借记
200
01/09/2010
借记
400
01/11/2010
借记
100


非常感谢。除了最后一个数组外,这段代码对每一行都非常有效。如何从$last_date变量中获取最新日期?我不知道,没有php经验。很抱歉。好的,没问题。我可以通过添加一个带有最后一个“To”日期的隐藏行来解决。@kritzikrazi。。你说得对,它更优雅;)谢谢