在二维数组jquery中存储表
我试图将一个表存储在一个2d数组中,这样数组中的每个项都将是一个包含一行中所有单元格的数组 我正在使用:在二维数组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
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] ]