Javascript Queryselector从html表格中创建一个对象数组,表格标题和标题在td中

Javascript Queryselector从html表格中创建一个对象数组,表格标题和标题在td中,javascript,html,queryselector,Javascript,Html,Queryselector,我的HTML表的结构如下: 让htmlToArray=(tableElement)=>{ const thead=tableElement.querySelector('thead'); const tbody=tableElement.querySelector('tbody'); //假设只有一个标题行 const columns=thead.querySelectorAll('tr-th'); 常量columnNames=[]; columns.forEach(函数(c){ 列名。推({

我的HTML表的结构如下:

让htmlToArray=(tableElement)=>{
const thead=tableElement.querySelector('thead');
const tbody=tableElement.querySelector('tbody');
//假设只有一个标题行
const columns=thead.querySelectorAll('tr-th');
常量columnNames=[];
columns.forEach(函数(c){
列名。推({
href:c.querySelector(“a”)?.getAttribute(“href”),
标题:c.querySelector(“a”)?.getAttribute(“标题”)
})
});
常量结果=[];
const bodyRows=tbody.querySelectorAll('tr');
bodyRows.forEach(r=>{
const tds=r.querySelectorAll('td');
const tdth=r.querySelector('th');
const obj={name:null,profile:null,imagAlt:null,imgTitle:null};
设res={};
//卡在此处无法获取th和href属性值正在重复,它正在提取tr中不存在的项的值
tds.forEach((td,idx)=>{
obj.name=tdth.innerText
obj.profile=tdth.querySelector(“a”)?.getAttribute(“href”)
obj.imagAlt=td.querySelector(“img”)?.getAttribute(“alt”)
obj.imgTitle=td.querySelector(“img”)?.getAttribute(“title”)
res=columnNames.map(i=>Object.assign(i,obj));
});  
console.log(res)
});
返回结果;
};
let table=document.querySelector(“#完成进度”);
log(JSON.stringify(htmlotarray(table))

/姓
电子邮件地址
johnb@gmail.com
Yrionl@gmail.com