我有一些 JSON 被拉入并读入一个对象@items
:
[
{
{
"id": "A",
"description": "a_description_one"
"value": some_alphanumeric_string
},
{
"id": "B",
"description": "b_description_one"
"value": some_alphanumeric_string
},
{
"id": "C",
"description": "c_description_one"
"value": some_alphanumeric_string
}
},
{
{
"id": "C",
"description": "c_description_3"
"value": some_alphanumeric_string
},
{
"id": "A",
"description": "a_description_3"
"value": some_alphanumeric_string
},
{
"id": "B",
"description": "b_description_3"
"value": some_alphanumeric_string
}
},
...
]
我的目标是在 HTML 中输出两个表格,如下所示:
-----------------------------------------------------
| A | B | C |
|-----------------------------------------------------|
|a_description_one|b_description_one|c_description_one|
|-----------------------------------------------------|
|a_description_two|b_description_two|c_description_two|
|-----------------------------------------------------|
|a_description_3 |b_description_3 |c_description_3 |
-----------------------------------------------------
第二张桌子:
----------------------------------------------------
| C value | C description | Count | Change |
|----------------------------------------------------|
|some string|it's description|times appeared| change |
----------------------------------------------------
虽然第一个表相当简单,但由于我不知道描述符的顺序,我无法找到对其进行编码的好方法。因此,我有:
<table>
<thead>
<tr>
<th>a</th>
...
</tr>
</thead>
<tbody>
<% for item in @items %>
<% for portion in item %>
<% if portion[0]["id"] == "A" %>
<% a = portion[0] %>
<% end %>
...
<% end %>
<tr>
<td><%= a["description"].to_s %></td>
...
</tr>
<% end %>
</tbody>
</table>
这看起来很糟糕。
至于第二个表,我要做的就是查看所有 C 值,如果有多个具有相同 C 值的倍数,则将它们连接起来。我还想加载昨天的数据,我可以从服务器获取并拥有 as @items_old
,并将具有相同值的项目数与昨天的数进行比较。我不知道该去哪里。