javascript - 从 pug 中的 JSON 对象正确创建表
问题描述
我正在尝试将我的 JSON 对象分解为它们的各个元素并动态填充表格。)
我猜想用哈巴狗做一种地图(动态填充表格。
tr
td.resultLog
- let heatResult1Log = result.heat1Logs;
for heatItem in heatResult1Log
for item of heatItem
#{item}
tr
以下是将结果发送给 pug 的代码:
return {
"Failures": {
"coolCount1":coolFail1Rows.length/2 , coolCheck1Index:coolFails1Array,cool1Logs:coolFails1Logs,
"heatCount1":heatFail1Rows.length/2 , heat1Logs:heatFails1Logs
}};
}
这是我的 JS 代码返回的对象(暂时忽略加热方面,因为答案是相同的)
{ Failures:
{ coolCount1: 2,
coolCheck1Index: [ 8865, 8866, 9077, 9078 ],
cool1Logs:
[ '[8865,"2019-03-14T04:00:00.000Z","3:10:00","cool","compressorCoolStage1On","auto","Sleep",74,74,75.3,"end"]',
'[8866,"2019-03-14T04:00:00.000Z","3:15:00",null,null,null,null,null,null,null,"end"]',
'[9077,"2019-03-14T04:00:00.000Z","20:50:00","cool","compressorCoolOff","auto","Home",76.8,76.8,77.3,"end"]',
'[9078,"2019-03-14T04:00:00.000Z","20:55:00",null,null,null,null,null,null,null,"end"]' ],
} }
我尝试映射到单个对象(因为我希望它看起来像普通表)
table.resultHeader
tr
th Index
th Date
th Time
th System Setting
th System Mode
th Calendar Event
th Program Mode
th Cool Set Temperature
th Heat Set Temperature
tr
tr
td.resultLog
- let heatResult1Log = result.heat1Logs;
for heatArray in heatResult1Log
for item in heatArray
td #{item}
tr
它看似一个字母一个字母地返回数组。
现在从技术上讲我明白了,因为结果是作为数组的数组发送的,但是有什么选择吗?我希望我的表和索引对齐,它作为数组数组而不是单个元素的数组返回。
我的预期结果是它看起来像一张普通的桌子,即
<table style="width:100%">
<tr>
<th>Index</th>
<th>Date</th>
<th>Time</th>
(etc)
</tr>
<tr>
<td>0</td>
<td>Sept 2</td>
<td>5:30</td>
</tr>
</table>
在每个“内部数组”的末尾(因为它是一个带有数组的数组),它将开始一个新行并为下一个数组填充元素。这就是现在的样子......(索引应该只有每个数组的第一个元素,第二个的日期等等)
我添加了一个示例 - 数字 5067 应该出现在索引下(作为返回的索引),日期应该出现在日期下。
如果我在循环中放置一个循环,就会发生这种情况:
解决方案
cool1Logs:
[ '[8865,"2019-03-14T04:00:00.000Z","3:10:00","cool","compressorCoolStage1On","auto","Sleep",74,74,75.3,"end"]',
'[8866,"2019-03-14T04:00:00.000Z","3:15:00",null,null,null,null,null,null,null,"end"]',
'[9077,"2019-03-14T04:00:00.000Z","20:50:00","cool","compressorCoolOff","auto","Home",76.8,76.8,77.3,"end"]',
'[9078,"2019-03-14T04:00:00.000Z","20:55:00",null,null,null,null,null,null,null,"end"]' ],
您正在迭代的对象是字符串数组。
'[8865,"2019-03-14T04:00:00.000Z","3:10:00","cool","compressorCoolStage1On","auto","Sleep",74,74,75.3,"end"]',
此代码工作正常,它将为您提供字符串中的字符。
for item in heatArray
td #{item}
您需要将其作为数组传递给 pug 模板。像下面
[8865,"2019-03-14T04:00:00.000Z","3:10:00","cool","compressorCoolStage1On","auto","Sleep",74,74,75.3,"end"]
推荐阅读
- scala - 我可以使用 memoization 来缓存来自 spark 作业的 hbase 读写数据吗?
- python - python 3中具有多个输入的函数
- swiftui - SwiftUI 不使用 UIViewRepresentable 和 ObservableObject 刷新我的自定义 UIView
- c# - 如何仅在 Tesseract C# 中捕获数字
- ti-basic - 试图显示矩阵编辑器的位置
- linkedin - LinkedIn 访问基本资料
- java - 如果增加迭代次数,顺序流比并行流快
- json - 我需要使用 powershell 从 Json 文件中获取特定值
- php - 使用 zend_call_function() 时的段错误
- java - 二维数组作为类变量