Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在二维数组jquery中存储表_Jquery_Arrays_Html Table_2d - Fatal编程技术网

在二维数组jquery中存储表

在二维数组jquery中存储表,jquery,arrays,html-table,2d,Jquery,Arrays,Html Table,2d,我试图将一个表存储在一个2d数组中,这样数组中的每个项都将是一个包含一行中所有单元格的数组 我正在使用: var tRow = []; var tRows = []; tab = document.getElementById('table'); for(var r = 0 ; r < tab.rows.length ; r++) { for (var c=0; c < tab.rows[r].cells.length; c++){ tRow[c] = t

我试图将一个表存储在一个2d数组中,这样数组中的每个项都将是一个包含一行中所有单元格的数组

我正在使用:

var tRow = [];
var tRows = [];
tab = document.getElementById('table'); 

for(var r = 0 ; r < tab.rows.length ; r++) {
    for (var c=0; c < tab.rows[r].cells.length; c++){
        tRow[c] = tab.rows[r].cells[c].innerHTML;                       
    }

    tRows.push(tRow);
}
而不是:

tRows =[ [Row 1 cell 1,Row 1 cell 2,Row 1 cell 3,Row 1 cell 4] , [Row 2 cell 1,Row 2 cell 2,Row 2 cell 3,Row 2 cell 4] ]

我不知道我做错了什么。请提供帮助。

您可以使用两个
map()
函数来完成此操作

var tRows=$('tr').map(函数(){
返回[$(this).find('td').map(函数(){
返回$(this.text())
}).get()]
}).get()
console.log(tRows)

第1行第1单元
第1行第2单元
第1行第3单元
第1行第4单元
第2行第1单元
第2行第2单元
第2行第3单元
第2行第4单元

您的问题是,对于外部循环的每次迭代,您都需要一个新的
tRow
数组

只需移动
var tRow=[]在该行循环中

var-tRows=[];
tab=document.getElementById('table');
对于(var r=0;r

第1行第1单元
第1行第2单元
第1行第3单元
第1行第4单元
第2行第1单元
第2行第2单元
第2行第3单元
第2行第4单元
tRows =[ [Row 2 cell 1,Row 2 cell 2,Row 2 cell 3,Row 2 cell 4] , [Row 2 cell 1,Row 2 cell 2,Row 2 cell 3,Row 2 cell 4] ]
tRows =[ [Row 1 cell 1,Row 1 cell 2,Row 1 cell 3,Row 1 cell 4] , [Row 2 cell 1,Row 2 cell 2,Row 2 cell 3,Row 2 cell 4] ]