简体   繁体   English

如何比较两个数组

[英]how to compare two arrays

I've an array - $scope.question = []; 我有一个数组- $scope.question = []; which store data which get from web api: id, question and correctAnswer 哪些存储从Web API获取的数据:ID,问题和CorrectAnswer

        quizService.getQuestions(id).success(function (data) {
            $scope.question = data;
        });

And, I've another array, which store answered question by user: 而且,我还有另一个数组,用于存储用户回答的问题:

    $scope.binding = {
        answers: {}
    };

    $scope.setAnswer = function (answer, id) {
        $scope.binding.answers[id] = answer;
    };

The question is, how to get data from array and compare them? 问题是,如何从数组中获取数据并进行比较?

    $scope.question[id]
    $scope.question[correctAnswer]

Will be this correct realization? 这是正确的实现吗?

   if ($scope.binding.answers[id] == $scope.question[id] && $scope.binding.answer[answer] == $scope.question[correctAnswer]) {
          ...
    }

PS I'm newbie in angularjs. PS我是angularjs的新手。

update 1: some more data function controller($scope, quizService) { 更新1:更多数据功能控制器($ scope,quizService){

    $scope.currentAnswer = null;
    //Set the answer from radio buttons
    $scope.setAnswer = function (answer, id) {
        $scope.binding.answers[id] = answer;
    };
    $scope.question = [];
    $scope.binding = {
        answers: {}
    };

    updQuestion();

    function updQuestion() {
        var id = 1;

       //Get the question
        quizService.getQuestions(id).success(function (data) {
            $scope.question = data;
        });

        //Button to get next question
        $scope.nextQuestion = function () {
            id++;
            quizService.getQuestions(id).success(function (data) {
                $scope.question = data;
                $scope.currentAnswer = $scope.binding.answers[id];
            });
        };

     //Submit button when user passed all questions
     $scope.onSubmit = function () {

            quizService.postResult().sucess(function () {

                for (var i = 0; i < $scope.question.length; i++) {

                    if ($scope.binding.answers[id] == $scope.question[id] && $scope.binding.answer[answer] == $scope.question[correctAnswer]) {
                        numbOfCorrectAnswers++;
                    }
                    $scope.question.push(numbOfCorrectAnswers);
                }
            });
        };

Update 2 更新2

So, I've tryed with 2 for loop and forEach, anyway can't get values from the objects in arrays. 因此,我尝试使用2 for loop和forEach,无论如何都无法从数组中的对象获取值。

 $scope.onSubmit = function () {
                var numbOfCorrectAnswers = 0;
                var answers = null;
                var n = 0;
                var b = 0;
                var questions;
                for (n = 0; n < $scope.getQuestions.lenght; n++) {
                    for (b = 0; b < $scope.binding.answers.lenght; b++) {
                    var questions = $scope.getQuestions[n];
                    var getAnswer = $scope.binding.answer[n];
                    if (questions.Id == getAnswer.id && questions.CorrectAnswer == getAnswer.answer) {
                        numbOfCorrectAnswers++;
                    }

                });

You can compare both arrays like this: 您可以像这样比较两个数组:

$scope.name.Xarray[i] == $scope.name.Yarray[n] , where i is a number in array the position. $scope.name.Xarray[i] == $scope.name.Yarray[n] ,其中i是数组中位置的数字。

Then you have to know that you have the iterate both arrays. 然后,您必须知道自己具有两个数组的迭代。 You can use for or while loop. 您可以使用forwhile循环。 Example: 例:

  for ( n in $scope.Xarray.lenght ) {
    for ( i in $scope.Yarray.lenght ) {
      Xarray[n] == Yarray[i]
    }
  }

It's only an example and doesn't mean that is exactly like this, but you go ahead with these info. 这只是一个示例,并不意味着就完全一样,但是您可以继续阅读这些信息。

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

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