我试图用问答来迭代json。对于每个问题,我有多个答案。我想用多个答案显示每个问题。我想展示。提前感谢您的帮助。
q1
a1.1
a1.2
q2
a2.1
a2.3
q3
a3.1
a3.2
a3.3
json看起来像:
[{"QuestionID":101,"Question":"q1","Response":"a1.1","CorrectResponse":false},
{"QuestionID":101,"Question":"q1","Response":"a1.2","CorrectResponse":true},
{"QuestionID":102,"Question":"q2","Response":"a2.1","CorrectResponse":false},
{"QuestionID":102,"Question":"q2","Response":"a2.2","CorrectResponse":true},
{"QuestionID":103,"Question":"q3","Response":"a3.1","CorrectResponse":false},
{"QuestionID":103,"Question":"q3","Response":"a3.2","CorrectResponse":true},
{"QuestionID":103,"Question":"q3","Response":"a3.3","CorrectResponse":true}]
这是我目前的代码
var questions = [];
var answersWithCorrectCheck = [];
questions.push(test[0].Question);
answersWithCorrectCheck.push({ answer: test[0].Response, correct: test[0].CorrectResponse });
for (i = 1; i < test.length; i++)
{
if(test[i - 1].QuestionID !== test[i].QuestionID )
{
questions.push(test[i].Question);
}
answersWithCorrectCheck.push({ answer: test[i].Response, correct: test[i].CorrectResponse });
}
$scope.displayQuestionsPerTest = questions;
$scope.answersWithCorrectResonse = answersWithCorrectCheck;
答案 0 :(得分:3)
您可以使用 angular-filter ,
执行此操作<body ng-app="DemoApp" ng-controller="DemoController">
<ul ng-repeat="(key, value) in data | groupBy: 'Question'">
Question name: {{ key }}
<li ng-repeat="answer in value">
{{ answer.Response }}
</li>
</ul>
</body>
<强> DEMO 强>