2016-10-31 11 views
0

私はtraviaのゲームアプリを構築することによってnativescriptを学びます。 私は以下のような質問JSONリストがあります:それは動作します最初は未定義の "userResponse"プロパティを取得できません

questionViewModel.startNewGame = function() { 


    for (var i = 0; i < questionViewModel.numberOfQuestions - 1; i++) { 
     questionViewModel.test.questions[i].userResponse = ""; 
     questionViewModel.test.questions[i].grade = ""; 
    } 
} 

が、後、私は未定義のプロパティuserResponseを設定することはできませんエラーが表示されます。私はこれをやっている個々の質問を取得するために

var questionsList20 = {"questions" : [{"question":"Taxing a commodity at the production level decreases supply.","correctAnswer":"answerOne","answerOne":"True","answerTwo":"False","answerThree":"Uncertain","answerFour":"None of the above","userResponse":"","explanationTwo":"True","grade":""},{"question":"The circular flow depicts the alternative combination of goods and services an economy \tcan produce at a point in time.","correctAnswer":"answerTwo","answerOne":"True","answerTwo":"False","answerThree":"Uncertain","answerFour":"None of the above","userResponse":"","explanationTwo":"False","grade":""},{"question":"Demand is inelastic if elasticity of demand < 0.","correctAnswer":"answerTwo","answerOne":"True","answerTwo":"False","answerThree":"Uncertain","answerFour":"None of the above","userResponse":"","explanationTwo":"False","grade":""},{"question":"Elasticity of demand measures the percentage change in the quantity demand of a commodity as a result of a given percentage change in price.","correctAnswer":"answerTwo","answerOne":"True","answerTwo":"False","answerThree":"Uncertain","answerFour":"None of the above","userResponse":"","explanationTwo":"False","grade":""}]} 

を。

私はこれを解決する方法がわかりません。助けてください。

+0

questionViewModel.numberOfQuestions' 'どこから来た私は知らないが、私はあなたがする必要はありませんだと思う - の条件は、私が'であるので、1 < '.. questionViewModelは何 –

+0

ありますか? – Bic

+0

questionViewModelは質問ビューのモデルです。 numberOfQuestionsはそれに含まれる質問の数です。 @ルーカスコスタ私は1をする必要はないという意味ですか? expantiateしてください。 – Joel

答えて

0

空の質問配列を初期化し、各質問を質問オブジェクトのように押すことができます。

questionViewModel.startNewGame = function() { 
    questionViewModel.test.questions = []; 

    for (var i = 0; i < questionViewModel.numberOfQuestions; i++) { 
     questionViewModel.test.questions.push({"userResponse": "", "grade": ""}); 
    } 
} 
+0

ありがとうalog @ニックllievそれは修正されました – Joel

関連する問題