Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/37.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
Javascript 从对象创建HTML表_Javascript - Fatal编程技术网

Javascript 从对象创建HTML表

Javascript 从对象创建HTML表,javascript,Javascript,我有一个目标: { type : "consultations/get", payload : [ {name : "bob", favoriteColor : "blue", age : 54}, {name : "steve" favoriteColor : "red" age : 45}, {name : "sarah&qu

我有一个目标:

{ 
 type : "consultations/get",
 payload : [
   {name : "bob",
    favoriteColor : "blue",
    age : 54},
   {name : "steve"
    favoriteColor : "red"
    age : 45},
    {name : "sarah",
    favoriteColor : "green",
    age : 38}
 ] 
}
如何获取name和favoriteColor键和值并映射到html表?我不想显示年龄,我希望能够设置显示哪些行(有效负载键)(即favoriteColor和name),并将相应的值映射到表单元格。任何帮助都将不胜感激。先谢谢你。以下是我迄今为止采取的方法:

var json = {
 type : "consultations/get",
 payload : [
   {name : "bob",
    favoriteColor : "blue",
    age : 54},
   {name : "steve",
    favoriteColor : "red",
    age : 45},
    {name : "sarah",
    favoriteColor : "green",
    age : 38}
 ] 
};

var tableHeader = `
${Object.keys(json.payload[0]).filter(e => e !== "age").map((row) => `
<th>${row}</th>`).join('')}`;
var json={
键入:“咨询/获取”,
有效载荷:[
{姓名:“鲍勃”,
最喜欢的颜色:“蓝色”,
年龄:54},
{姓名:“史蒂夫”,
最喜欢的颜色:“红色”,
年龄:45},
{姓名:“莎拉”,
最喜欢的颜色:“绿色”,
年龄:38}
] 
};
var tableHeader=`
${Object.keys(json.payload[0]).filter(e=>e!==“age”).map((行)=>`
${row}`);

tableHeader很好,我只是停留在如何过滤和映射数据上。在做了tableHeader之后,我会循环通过有效载荷并相应地添加td吗?如何使用我在键上放置的特定筛选器正确执行此操作?

您可以使用数据构建一个表字符串,并将其插入html中

const数据={
键入:“咨询/获取”,
有效载荷:[
{姓名:“鲍勃”,
最喜欢的颜色:“蓝色”,
年龄:54},
{姓名:“史蒂夫”,
最喜欢的颜色:“红色”,
年龄:45},
{姓名:“莎拉”,
最喜欢的颜色:“绿色”,
年龄:38}
] 
}
let table=“NameFav Color”
data.payload.forEach((x,ind)=>{
log(“这里是对应的行”+(ind+1))
表+=`${x.name}${x.favoriteColor}`
})
表+=“”
document.getElementById(“tab”).innerHTML=table
试试这个:

const response={
键入:“咨询/获取”,
有效载荷:[
{
姓名:“鲍勃”,
最喜欢的颜色:“蓝色”,
年龄:54岁,
},
{
姓名:“史蒂夫”,
最喜欢的颜色:“红色”,
年龄:45,,
},
{
姓名:“莎拉”,
最喜欢的颜色:“绿色”,
年龄:38,
},
],
};
常量显示={
姓名:对,
favoriteColor:对,
年龄:错,
}
const keys=Object.keys(keystshow);
const generateTable=()=>{
const body=document.querySelector('body');
const table=document.createElement('table');
addTableHead(表);
addTableRows(表);
body.appendChild(表);
}
const addTableHead=(表)=>{
const tableRow=document.createElement('tr');
key.forEach((key)=>{
如果(显示[键]){
addTableCell(tableRow,键,'th');
}
});
table.appendChild(tableRow);
}
const addTableRows=(表)=>{
response.payload.forEach((项目)=>{
const tableRow=document.createElement('tr');
key.forEach((key)=>{
如果(显示[键]){
addTableCell(tableRow,项[键]);
}
});
table.appendChild(tableRow);
});
}
const addTableCell=(tableRow,content,type='td')=>{
const tableCell=document.createElement(类型);
const tableCellContent=document.createTextNode(内容);
tableCell.appendChild(tableCellContent);
tableRow.appendChild(tableCell);
} 
document.querySelector(“#generate”).addEventListener('click',generateTable)

Generate Table
请参见此处(纯JS):或此处(jQuery):谢谢您提供的信息:)谢谢您提供的信息。我来试试:)谢谢你Dfvc:)