gpt4 book ai didi

javascript - 将嵌套的 JSON 数据转换为表

转载 作者:搜寻专家 更新时间:2023-11-01 04:13:02 25 4
gpt4 key购买 nike

我正在尝试将嵌套的 JSON 数据转换为 HTML 表格,但它一直抛出错误。

我不知道我哪里做错了。也许访问对象内部数组的方法有问题?

一直报错:

"Cannot set property 'innerHTML' of null"

下面是我写的代码:

function DonutTable(array){
//create a table element
var table = document.createElement("table");
//create header columns

var col = Object.keys(array[0]); //array of keys
//write keys onto the header cell
var tr = table.insertRow(-1);
col.forEach(function(key){
var th = document.createElement("th");
th.textContent = key;
tr.appendChild(th);
});

//create rows to hold the rest of the data
array.forEach(function(obj){
//for each obj in the main array, create a row
var data_row = table.insertRow(-1);
//for each header in the col array, populate data
col.forEach(function(key){
var tabCell = data_row.insertCell(-1);
if (key==="batters"){
//grab the value of batters and access value of batter
obj["batters"]["batter"].forEach(function(e){
//for each e in batter, create a div element
var div = document.createElement("div");
//write on the div
div.textContent = e.type + "(" + e.id + ")";
tabCell.appendChild(div); })
}
if (Array.isArray(obj[key])){ //check if a value of a key is an array
obj[key].forEach(function(topping){
//for each obj in topping, get id and type
var div = document.createElement("div");
div.textContent = topping.type + "(" + topping.id + ")";
tabCell.appendChild(div);
})
}
else{
tabCell.textContent = obj[key];
}


})
})


var divContainer = document.getElementById("showTable");
divContainer.innerHTML = "";
divContainer.appendChild(table);

}

var donut = [
{
"id": "0001",
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
{ "id": "1002", "type": "Chocolate" },
{ "id": "1003", "type": "Blueberry" },
{ "id": "1004", "type": "Devil's Food" }
]
},
"topping":
[
{ "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5005", "type": "Sugar" },
{ "id": "5007", "type": "Powdered Sugar" },
{ "id": "5006", "type": "Chocolate with Sprinkles" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
]
},
{
"id": "0002",
"type": "donut",
"name": "Raised",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" }
]
},
"topping":
[
{ "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5005", "type": "Sugar" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
]
},
{
"id": "0003",
"type": "donut",
"name": "Old Fashioned",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
{ "id": "1002", "type": "Chocolate" }
]
},
"topping":
[
{ "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
]
}
]
DonutTable(donut);

<html>
<head>
<title>HTML Donut Table from JSON</title>
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<input type="button" value="Generate a table" onclick="DonutTable()">
<div id="showTable"></div>
</body>
</html>

最佳答案

在 Chrome 中,在声明 divContainer 后立即设置断点。根据代码,看起来 divContainer 为空,因为您在页面上的 HTML 之前运行 JavaScript。要么将 JS 移动到 document.ready 类型的函数中,要么将脚本部分移动到 HTML 下方。

关于javascript - 将嵌套的 JSON 数据转换为表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47944924/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com