简体   繁体   中英

how to compare two arrays

I've an array - $scope.question = []; which store data which get from web api: id, question and 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.

update 1: some more data function controller($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

So, I've tryed with 2 for loop and forEach, anyway can't get values from the objects in arrays.

 $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.

Then you have to know that you have the iterate both arrays. You can use for or while loop. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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