Quantcast
Channel: Ionic Forum - Latest topics
Viewing all articles
Browse latest Browse all 70747

Pushing nested and manipulated array Data onto another array

$
0
0

@wakadala wrote:

I have been going over this for some time and I cant seem to get it working as required.

What I have:

  1. I have an array of questions under which is a nested array of answers like this:
    0: question 1
    answers:
    0:
    answer_id: “1”
    ans_index: “A”
    1:
    answer_id: “2”
    ans_index: “B”
    1: question 2
    etc…

What I need to do:

  1. I need to collect “ans_index” for all answers under each question so I have something like "chosen_index: “A, B, C …”
  2. Push each array of indices under each question.

What I get:
With the function below, I get everything right except that the values for "ans_index include everything from all answers in all questions rather than answers under each question.

This is my component TS Function:

  testAnswers(){

    this.my_answers = this.questionWanswers; //contains all questions - each with array of answers

    for(var k=0; k<this.my_answers.length; k++){
      this.current_question = this.my_answers[k]; //questions 

      this.qn_answers = this.my_answers[k].answers; //answers array

        for(var l=0; l<this.qn_answers.length; l++){
          this.qn_answers[l].selected = true; //not important for now
          
            this.chosen_index.push([l] + " " + this.qn_answers[l].ans_index); // PROBLEM: I need to create an array of ans_index for each question (one item from each answer);
            
          }

        this.current_question.order.push({"chosen_order" : this.chosen_index}); //pushing the array of ans_index onto each separate question, instead - all questions receive the same ans_index array combined from all questions
        
        }

        console.log('My answers', this.my_answers);
        // save to db
      //  this.saveTest();
    }

This what is recorded in the console:
58

From the data in the image above: chosen_order should be 0 - 2 under question 1 because it has 3 answers, and 3-6 because question two has 4 answers but all seven show up under all 2 questions.

Please help. I can elaborate further if need be. Thanks!

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 70747

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>