作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试用json对象制作表格,实际上我有大量的JSON对象。但首先,我尝试为几个JSON对象创建tabe,如果一切顺利,我将在稍后实现此方法。
这是我到目前为止尝试过的。
var attributes =
[
{
"name": "Asset #",
"display_name": "Asset #",
"key": "header2",
"values": {
"MSSBL": "4-194169767930##1-2H77NZSQ",
"SNOW": "4-194169767930##1-2H77NZSQ"},
"type": "header",
"data_type": "Text",
"editable": "N"
},
{
"name": "Serial Number",
"display_name": "Serial Number",
"key": "header3",
"values": {
"MSSBL": "21256112##1-2H77NZSQ",
"SNOW": "NA##1-2H77NZSQ"},
"type": "header",
"data_type": "Text",
"editable": "N"
},
{
"name": "ACCOUNT NUMBER",
"display_name": "ACCOUNT NUMBER",
"key": "header6",
"values": { "MSSBL": "532649##1-2H77NZSQ",
"SNOW": "NA##1-2H77NZSQ"},
"type": "header",
"data_type": "Text",
"editable": "N"
}
]
var key = [];
document.write("<table border==\"1\"><tr>");
for (key in attributes[0]) {
document.write('<td>' + '<b>' + key + '</b>' + '</td>');
}
document.write("</tr>");
for (var i = 0; i < attributes.length; i++) {
document.write('<tr>');
for (key in attributes[i]) {
document.write('<td>' + attributes[i][key] + '</td>');
}
document.write('</tr>');
}
document.write("</table>");
"values": {
"MSSBL": "4-194169767930##1-2H77NZSQ",
"SNOW": "4-194169767930##1-2H77NZSQ"},
最佳答案
您的麻烦来自此循环:
for (var j = 0; j < col.length; j++) {
// ...
}
col[j]
是
values
何时获得
MSSBL
或
SNOW
值:
for (var j = 0; j < col.length; j++) {
var tabCell = tr.insertCell(-1);
if (col[j] === 'values') {
var item = attributes[i][col[j]];
console.log('MSSBL:', item.MSSBL);
console.log('SNOW:', item.SNOW);
// try to append MSSBL value:
tabCell.innerHTML = item.MSSBL;
} else {
// other properties
tabCell.innerHTML = attributes[i][col[j]];
}
}
for (key in attributes[i]) {
if (key === 'values') {
document.write('<td>' + attributes[i][key]['MSSBL'] + '</td>');
} else {
document.write('<td>' + attributes[i][key] + '</td>');
}
}
var attributes =
[
{
"name": "Asset #",
"display_name": "Asset #",
"key": "header2",
"values": {
"MSSBL": "4-194169767930##1-2H77NZSQ",
"SNOW": "4-194169767930##1-2H77NZSQ"},
"type": "header",
"data_type": "Text",
"editable": "N"
},
{
"name": "Serial Number",
"display_name": "Serial Number",
"key": "header3",
"values": {
"MSSBL": "21256112##1-2H77NZSQ",
"SNOW": "NA##1-2H77NZSQ"},
"type": "header",
"data_type": "Text",
"editable": "N"
},
{
"name": "ACCOUNT NUMBER",
"display_name": "ACCOUNT NUMBER",
"key": "header6",
"values": { "MSSBL": "532649##1-2H77NZSQ",
"SNOW": "NA##1-2H77NZSQ"},
"type": "header",
"data_type": "Text",
"editable": "N"
}
]
var key = [];
document.write("<table border==\"1\"><tr>");
for (key in attributes[0]) {
document.write('<td>' + '<b>' + key + '</b>' + '</td>');
}
document.write("</tr>");
for (var i = 0; i < attributes.length; i++) {
document.write('<tr>');
for (key in attributes[i]) {
if (key === 'values') {
document.write('<td>' + attributes[i][key]['MSSBL'] + '</td>');
} else {
document.write('<td>' + attributes[i][key] + '</td>');
}
}
document.write('</tr>');
}
document.write("</table>");
关于javascript - 如何从JSON对象创建表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60257194/
我是一名优秀的程序员,十分优秀!