作者:下页楠哥_768 | 来源:互联网 | 2023-01-30 14:48
IhavetwotablesthatIneedtoextractdatafrom,andturnthisintoajavascriptobjectstructure
I have two tables that I need to extract data from, and turn this into a Javascript object structure, ideally like the following: Data --> Table --> Rows --> Cells. The tables are in the following format.
我有两个表需要从中提取数据,并将其转换为Javascript对象结构,理想情况如下:数据 - >表 - >行 - >单元格。表格采用以下格式。
My Javascript and jQuery so far has not had the desired effect:
到目前为止,我的Javascript和jQuery没有达到预期的效果:
$(document).ready(function(){
gatherData();
});
function gatherData(){
data = [];
tables = $('.before').find('table');
$(tables).each(function(index){
var table = [];
var rows = [];
var headRow = $(this).find('tr:first');
var names = [];
$(headRow).find('td').each(function(){
var name = $(this).text();
names.push(name)
});
$(this).find('tr').each(function(index){
if (index != 0){
rows.push({rowNum: index})
}
})
table.push(names)
table.push({rows: rows})
console.log(table)
data.push({table: index})
});
}
Here is a jsfiddle I have created.
这是我创建的一个jsfiddle。
1 个解决方案