-3
我想在問題和答案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;
請向我們展示一些代碼與您的實驗,我們可以嘗試。 – Andrea
謝謝我試圖將json分成兩個單獨的javascript數組。一個用於問題,另一個用於答案。我正在使用兩個ul列表。 – user6934713
問題解決了嗎?如果沒有,請分享代碼! –