gpt4 book ai didi

javascript - 如何将此 JSON 数据循环到表中?

转载 作者:行者123 更新时间:2023-11-27 23:27:00 27 4
gpt4 key购买 nike

我有这个 json 数据:

{
"particles": {
"name": "particles",
"values": [
["35.5", 1466588408869],
["35.5", 1466589538531],
["45.5", 1466589577084]
]
},
"timestamps": {
"name": "timestamps",
"values": [
["144500", 1466588408870],
["144500", 1466589538531],
["144500", 1466589577084]
]
}
}

如何将它循环到下表中?

<table>
<tr>
<th>particles</th>
<th>timestamps</th>
</tr>
<tr>
<td>35.5</td>
<td>144500</td>
</tr>
<tr>
<td>35.5</td>
<td>144500</td>
</tr>
<tr>
<td>45.5</td>
<td>144500</td>
</tr>
</table>

我的尝试:

div.table-responsive
table.table.table-hover.table-bordered
thead
tr
each variable, i in dataset.data
if variable.name
th.text-center #{variable.name}
tbody
tr
each variable, i in dataset.data
if variable.values
td
each value, i in variable.values
p= value[0]

结果(不好):

    <table class="table table-hover table-bordered">
<thead>
<tr>
<th class="text-center">particles</th>
<th class="text-center">timestamps</th>
</tr>
</thead>
<tbody>
<tr>
<td><p>35.5</p><p>35.5</p><p>45.5</p></td>
<td><p>144500</p><p>144500</p><p>144500</p></td>
</tr>
</tbody>
</table>

有什么想法吗?

最佳答案

我猜它很脏,但这里可能是答案的开始......总比没有好。

你可以通过这个让自己怀孕:

var data = {
"particles": {
"name": "particles",
"values": [
["35.5", 1466588408869],
["35.5", 1466589538531],
["45.5", 1466589577084]
]
},
"timestamps": {
"name": "timestamps",
"values": [
["144500", 1466588408870],
["144500", 1466589538531],
["144500", 1466589577084]
]
}
};

var titles = [];
var append = "";

$.each(data, function(i, e) {
titles.push(e.name);
});

append += "<tr>";
$.each(titles, function(i, title) {
append += "<th>" + title + "</th>";
});
append += "</tr>";

$.each(data[titles[0]].values, function(i, e) {
append += "<tr>";
$.each(titles, function(o, title) {
append += "<td>" + data[title].values[i][0] + "</td>";
});
append += "</tr>";
});

$("#result").append(append);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="result"></table>

关于javascript - 如何将此 JSON 数据循环到表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37967202/

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