繁体   English   中英

将数组作为属性从模块数组附加到模块吗?

[英]Attach an Array as an attribute to the Module from the Array of Modules?

我有两个对象数组:

  1. 模组
  2. 学习者回答的问题(常见问题)

在我的模块对象中,我有一个名为quiz的属性,其中包含一系列问题。 我想将此数组与学习者回答的问题(laq)(嵌套数组的数组)进行比较。 如果学习者回答问题(laq)的数组与模块数组中Module.quiz之一中的问题数组匹配,那么我想为其附加学习者回答问题(laq)的属性。 Modules对象的问题数组中的问题和“学员已回答的问题数组中的问题”具有文本的公共属性。 使用这个,我想比较两个数组是否相等,如果相等,则将学习者回答数组中的数组作为属性附加到Modules对象。

例如考虑:

Modules = [module_1, module_2, module_3]
module1.quiz.questions = [question_1, question_2, question_3]
module2.quiz.questions = [question_4, question_5, question_6]
module3.quiz.questions = [question_7, question_8, question_9]

Learners_Answered_QUestions = [
    [question_7, question_8, question_9], 
    [question_1, question_2, question_3], 
    [question_4, question_5, question_6]
]

我无法直接比较这些数组,因为它们具有不同的属性。 因此,我必须单独检查来自Learner_Answered_Questions数组之一的数组中的所有问题在Modules Quiz属性之一中是否都具有相同的问题。

这是我的代码:

for(var i = 0; i < $scope.modules.length; i++){
    for(var j = 0; j < $scope.laq.length; j++){
        if($scope.modules[i].quiz[0].questions.length === $scope.laq[j].length){
            var array_size = $scope.laq[j].length;
            for(var k = 0; k < array_size; k++){
                if($scope.modules[i].quiz[0].questions[k].text === $scope.laq[j][k].quiz_question.text){
                    $scope.attach = true;
                }
                else{
                    $scope.attach = false;
                };
            };
        };
        if($scope.attach === true){
            console.log($scope.attach);
            $scope.modules[i].lq_exists = true;
            $scope.modules[i].learner_quiz = $scope.laq[j];
            console.log($scope.modules[i]);
        }
    };
};

即使更改了数组中任何问题的顺序,我的代码也无法正常运行,因此我想使其更加可靠。

这是示例json格式:-

module1.quiz = {
"url": "http://localhost:8080/api/registration_quiz/38/",
"id": 38,
"quiz_name": "Rakesh BIO",
"module_referred": "http://localhost:8080/api/registration_courses_modules/98/",
"questions": [
    {
        "url": "http://localhost:8080/api/registration_quiz_questions/109/",
        "quiz": "http://localhost:8080/api/registration_quiz/38/",
        "q_type": "MCQ",
        "text": "What is Rakesh's profession",
        "possible_answers": [
            {
                "url": "http://localhost:8080/api/registration_answer_options/306/",
                "text": "cook"
            },
            {
                "url": "http://localhost:8080/api/registration_answer_options/307/",
                "text": "Accountant"
            },
            {
                "url": "http://localhost:8080/api/registration_answer_options/308/",
                "text": "IT"
            },
            {
                "url": "http://localhost:8080/api/registration_answer_options/309/",
                "text": "Plumber"
            }
        ],
        "selected": null,
        "correct": {
            "url": "http://localhost:8080/api/registration_answer_options/308/",
            "text": "IT"
        }
    },
    {
        "url": "http://localhost:8080/api/registration_quiz_questions/110/",
        "quiz": "http://localhost:8080/api/registration_quiz/38/",
        "q_type": "MCQ",
        "text": "What is his place's name?",
        "possible_answers": [
            {
                "url": "http://localhost:8080/api/registration_answer_options/310/",
                "text": "Yavatmal"
            },
            {
                "url": "http://localhost:8080/api/registration_answer_options/311/",
                "text": "Dhule"
            },
            {
                "url": "http://localhost:8080/api/registration_answer_options/312/",
                "text": "Sangamner"
            },
            {
                "url": "http://localhost:8080/api/registration_answer_options/313/",
                "text": "Solapur"
            }
        ],
        "selected": null,
        "correct": {
            "url": "http://localhost:8080/api/registration_answer_options/311/",
            "text": "Dhule"
        }
    }
]

}

和Learners_answered_questions JSON:-

lqa = [
    {
        "quiz_question": {
            "url": "http://localhost:8080/api/registration_quiz_questions/110/",
            "quiz": "http://localhost:8080/api/registration_quiz/38/",
            "q_type": "MCQ",
            "text": "What is his place's name?",
            "possible_answers": [
                {
                    "url": "http://localhost:8080/api/registration_answer_options/310/",
                    "text": "Yavatmal"
                },
                {
                    "url": "http://localhost:8080/api/registration_answer_options/311/",
                    "text": "Dhule"
                },
                {
                    "url": "http://localhost:8080/api/registration_answer_options/312/",
                    "text": "Sangamner"
                },
                {
                    "url": "http://localhost:8080/api/registration_answer_options/313/",
                    "text": "Solapur"
                }
            ],
            "selected": null,
            "correct": {
                "url": "http://localhost:8080/api/registration_answer_options/311/",
                "text": "Dhule"
            }
        },
        "learner": {
            "url": "http://localhost:8080/api/registration_learners/4/",
            "user": "http://localhost:8080/api/registration_custom_users/4/",
            "profile_picture": null,
            "courses_learning": "Django"
        },
        "chosen_option": {
            "url": "http://localhost:8080/api/registration_answer_options/250/",
            "text": "No Answer Selected"
        }
    },
    {
        "quiz_question": {
            "url": "http://localhost:8080/api/registration_quiz_questions/109/",
            "quiz": "http://localhost:8080/api/registration_quiz/38/",
            "q_type": "MCQ",
            "text": "What is Rakesh's profession",
            "possible_answers": [
                {
                    "url": "http://localhost:8080/api/registration_answer_options/306/",
                    "text": "cook"
                },
                {
                    "url": "http://localhost:8080/api/registration_answer_options/307/",
                    "text": "Accountant"
                },
                {
                    "url": "http://localhost:8080/api/registration_answer_options/308/",
                    "text": "IT"
                },
                {
                    "url": "http://localhost:8080/api/registration_answer_options/309/",
                    "text": "Plumber"
                }
            ],
            "selected": null,
            "correct": {
                "url": "http://localhost:8080/api/registration_answer_options/308/",
                "text": "IT"
            }
        },
        "learner": {
            "url": "http://localhost:8080/api/registration_learners/4/",
            "user": "http://localhost:8080/api/registration_custom_users/4/",
            "profile_picture": null,
            "courses_learning": "Django"
        },
        "chosen_option": {
            "url": "http://localhost:8080/api/registration_answer_options/306/",
            "text": "cook"
        }
    }
]

出于这个问题的目的,我将JSON简化为仅相关的内容。

在下面找到一个可以正常工作的算法(您可以四处浏览并更改长度或文本以检查所有情况)

 var modules = [ { "id": 38, "questions": [ { "text": "What is Rakesh's profession" }, { "text": "What is his place's name?" } ] }, { "id": 39, "questions": [ { "text": "What is Protozoid's profession" }, { "text": "What is his car's name?" } ] } ]; var laq = [ [{ "quiz_question": { "text": "What is his place's name?" } }, { "quiz_question": { "text": "What is Rakesh's profession" } }], [{ "quiz_question": { "text": "What is Protozoid's profession" } }, { "quiz_question": { "text": "This shouldnt be attached because not all questions match" } }] ]; modules.forEach(function (module) { var shouldAttach = true; var lqReference = []; laq.forEach(function (laqItem) { var matchedCount = 0; module.questions.forEach(function (moduleQuestion) { laqItem.forEach(function (laqItemQuestion) { // console.log(moduleQuestion.text, laqItemQuestion.quiz_question.text, moduleQuestion.text == laqItemQuestion.quiz_question.text); if (moduleQuestion.text == laqItemQuestion.quiz_question.text) { matchedCount++; } }); }); // console.log("should attach1", matchedCount, laqItem.length); if (matchedCount == laqItem.length) { shouldAttach = true; lqReference = laqItem; } else { matchedCount = 0; } // console.log("should attach2", shouldAttach); // If questions matched but different lengths if (shouldAttach && module.questions.length !== lqReference.length) { shouldAttach = false; } }); // console.log("should attach3", shouldAttach); if (shouldAttach) { module.lq_exists = true; module.learner_quiz = lqReference; } }); console.log(modules); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM