我有这个 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/