【发布时间】:2013-07-16 14:05:35
【问题描述】:
需要遍历以下 JSON 对象以生成报告(表格结构中的表格报告)。
报告基本上由故事列表及其相关任务状态组成。
QueryResults = {
"Results": [
{
"Name" : "Tech Design",
"State" : "Completed",
"StoryName" : "FB Integration"
},
{
"Name" : "Development",
"State" : "In-Progress",
"StoryName" : "FB Integration"
},
{
"Name" : "QA Testing",
"State" : "Not Started",
"StoryName" : "FB Integration"
},
{
"Name" : "Front End Development",
"State" : "Completed",
"StoryName" : "FB Integration"
},
{
"Name" : "Tech Design",
"State" : "Not Started",
"StoryName" : "Twitter Integration"
},
{
"Name" : "Development",
"State" : "Not Started",
"StoryName" : "Twitter Integration"
}
]
}
要填充的 HTML:
<table>
<tr>
<td>StoryName</td>
<td>TechDesign</td>
<td>FED</td>
<td>QA</td>
<td>Development</td>
</tr>
<tr>
<td>FB Integration</td>
<td>Completed</td>
<td>Completed</td>
<td>In-Progress</td>
<td>In-Progress</td>
</tr>
......
......
</table>
脚本:
for(var i=0; i < QueryResults.Results.length; i++) {
data+= '<tr><td>' + QueryResults.Results[i].StoryName + '</td><td>' + QueryResults.Results[i].State + '</td></tr>';
}
// it will list down all the story name i.e. same story name multiple times
请在迭代 JSON 并在其他列中填充相应的详细信息时向我提供删除重复故事名称的指针。
【问题讨论】:
-
重复项会发生什么?你想丢弃它们吗?如果是,你想保留哪一个?
-
我们需要从重复条目中选择任务名称和状态,避免打印故事名称显示两次。
标签: javascript jquery json loops for-loop