2014-10-09 83 views
-1

我想从给定的表中生成一个矩阵。如何生成矩阵?

但我不知道生成一个正确存储矩阵的变量。

示例表:

<table>  
    <tr class="csv-row"> 
     <td class="field-sort">Ashton Cox</td> 
     <td class="field-sort">[email protected]</td> 
     <td class="field-sort">01/06/1995</td> 
     <td class="field-sort">Visitante</td> 
     <td class="field-sort">01/10/2014</td> 
     <td class="field-sort">10/01/2014</td> 
    </tr> 
    <tr class="csv-row"> 
     <td class="field-sort">Bruno Nash</td> 
     <td class="field-sort">[email protected]</td> 
     <td class="field-sort">10/06/1988</td> 
     <td class="field-sort">Atacado</td> 
     <td class="field-sort">01/10/2014</td> 
     <td class="field-sort">10/01/2014</td> 
    </tr> 
</table> 

我的循环:

jQuery('tr.csv-row').each(function() { 
    jQuery(this).find('td.field-sort').each(function() { 

    }); 
}); 

我想返回我这样一个矩阵的变量:

Array[0] 
(
    [0] => Ashton Cox 
    [1] => [email protected] 
    [2] => 01/06/1995 
    [3] => Visitante 
    [4] => 01/10/2014 
    [5] => 10/01/2014 
), 
Array[1] 
(
    [0] => Bruno Nash 
    [1] => [email protected] 
    [2] => 10/06/1988 
    [3] => Atacado 
    [4] => 01/10/2014 
    [5] => 10/01/2014 
) 

我的例子不能正常工作:

var jsonArr = []; 
jQuery('tr.csv-row').each(function() {   
    jQuery(this).find('td.field-sort').each(function() { 
     jsonArr.push({     
      name: jQuery(this).text(), 
      email: jQuery(this).text(), 
      group: jQuery(this).text(), 
      born: jQuery(this).text(), 
      purchase: jQuery(this).text(), 
      created: jQuery(this).text() 
     }); 
    }); 
}); 

回答

0

如果你想“二维”数组,你可以修改你的代码到这个:

Fiddle

var jsonArr = []; 
jQuery('tr.csv-row').each(function(index) 
{ 
    jsonArr[index] = []; 
    jQuery(this).find('td.field-sort').each(function() 
    { 
     jsonArr[index].push(jQuery(this).text()); 
    }); 
}); 
console.log(jsonArr); 
1

检查此密码。

var jsonArr = []; 
 
var jsonInnerArr = []; 
 
    jQuery('tr.csv-row').each(function() {   
 
     jQuery(this).find('td.field-sort').each(function() { 
 
      jsonInnerArr.push(jQuery(this).text()); 
 
     }); 
 
     jsonArr.push(jsonInnerArr); 
 
     jsonInnerArr = []; 
 
     
 
    }); 
 

 
console.log(jsonArr);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table>  
 
     <tr class="csv-row"> 
 
      <td class="field-sort">Ashton Cox</td> 
 
      <td class="field-sort">[email protected]</td> 
 
      <td class="field-sort">01/06/1995</td> 
 
      <td class="field-sort">Visitante</td> 
 
      <td class="field-sort">01/10/2014</td> 
 
      <td class="field-sort">10/01/2014</td> 
 
     </tr> 
 
     <tr class="csv-row"> 
 
      <td class="field-sort">Bruno Nash</td> 
 
      <td class="field-sort">[email protected]</td> 
 
      <td class="field-sort">10/06/1988</td> 
 
      <td class="field-sort">Atacado</td> 
 
      <td class="field-sort">01/10/2014</td> 
 
      <td class="field-sort">10/01/2014</td> 
 
     </tr> 
 
    </table>

+0

好了,我们只能说,这是一样的,我已经公布。 – Regent 2014-10-09 12:14:39

+0

感谢您的帮助,我为您奉献 – Dexxtz 2014-10-09 12:32:39