2014-10-09 135 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