javascript对象的递归验证

时间:2015-01-24 01:09:14

标签: javascript json recursion

我在这里做错了吗?我尝试使用递归来验证以下JSON数据,但我想深入到已选择答案的级别。

修改

这意味着我想检查使用递归这个JSON对象,如果问题[index] .SelectedAnswers.length> 0如果选择了其中一个答案,则检查嵌套问题。

    {
    "AnswerType": "single-choice",
    "Answers": [
        {
            "AnswerValue": null,
            "Description": "Yes",
            "ID": 124804,
            "IsSelected": true,
            "Questions": [
                {
                    "AnswerType": "multi-choice",
                    "Answers": [
                        {
                            "AnswerValue": null,
                            "Description": "Answer text",
                            "ID": 124806,
                            "IsSelected": true,
                            "Questions": [
                                {
                                    "AnswerType": "singleline",
                                    "Answers": [
                                        {
                                            "AnswerValue": null,
                                            "Description": null,
                                            "ID": 124810,
                                            "IsSelected": false,
                                            "Questions": [],
                                            "$$hashKey": "object:230"
                                        }
                                    ],
                                    "Description": "Question text",
                                    "ID": 37894,
                                    "SelectedAnswers": [],
                                    "$$hashKey": "object:226",
                                    "IsValid": true
                                }
                            ],
                            "$$hashKey": "object:217"
                        }
                    ],
                    "Description": "Question text",
                    "ID": 37892,
                    "SelectedAnswers": [
                        124804
                    ],
                    "$$hashKey": "object:204",
                    "IsValid": false
                }
            ]
        }
    ]
}
验证此结构的

Here I've pasted my functions & extended version of JSON data。我试图让验证工作到达选择答案的级别

$scope.IsQuestionCompleted = function (question) {
    var answer;
    var completed;
    var qAnswer;

    if (question.AnswerType == 'geen') {
        question.IsSelected = true;
        $scope.setAnswer(question, question.Answers[0].ID);
    }

    if (question.AnswerType == 'multiline' || question.AnswerType == 'date' || question.AnswerType == 'singleline') {

        var answer;
        var isAnswerValueSelected;

        for (answer in question.Answers) {
            if (question.Answers[answer].ID == question.SelectedAnswers[0]) {
                if (question.Answers[answer].AnswerValue != null) {
                    isAnswerValueSelected = true;
                } else {
                    isAnswerValueSelected = false;
                }

                if (isAnswerValueSelected && question.SelectedAnswers && question.SelectedAnswers.length > 0)
                    completed = true;
                else
                    if ($rootScope.surveyQuestions.Questions.length > 1)
                        completed = false;
            }
        }
    } else {

        var iAnswer;
        for (iAnswer in question.Answers)
            if (question.Answers[iAnswer].ID == question.SelectedAnswers[0])
                if (question.Answers[iAnswer].Questions.length == 0)
                    return true;

        completed = question.SelectedAnswers && question.SelectedAnswers.length > 0;
    }

    question.IsValid = !completed;

    for (var i = 0; i < question.Answers.length; i++) {
        for (var j = 0; j < question.Answers[i].Questions.length; j++) {
            if (!$scope.IsQuestionCompleted(question.Answers[i].Questions[j]))
                completed = false;
        }
    }

    return completed;
}

//And the above function is called by:
$scope.isPageCompleted = function () {
    var isPageCompleted = true;
    var question;

    for (question in $rootScope.surveyQuestions.Questions) {
        if (!$scope.IsQuestionCompleted($rootScope.surveyQuestions.Questions[question]))
            isPageCompleted = false;
    }
    return isPageCompleted;
}

0 个答案:

没有答案