Mr Mohamed
Mr Mohamed

Reputation: 464

iterating through multi level array

i have questions i'm trying to iterate through , and every question might have subquestions , i wanna iterate through them all:- my variables are:-

                    Questions:[],
                    current_question : [],
                    check_box_answers: [],
                    got_subquestion: false,
                    sub_Questions :[],
                    iterator :  0,
                    subQuestion_iterator:-1,

Those are my data object:- and here is my function when i submit the value i wanna get the next question and check if it has subquestions or not, if it does, then iterate through the sub questions and when they finish get back to the main questions, here is what the json data look like:-

 {

            "title": "asd",
            "sub_questions": [],
            "section_title": "asdaa",
            "type": "radio",
            "answers": [
                {
                    "uuid": "56907C80-FD7D-4F83-8C25-3FBA1CD9A060",
                    "title": "1-3 asd"
                },
                {
                    "uuid": "A71EF3F5-2F02-44A2-ABC9-085D52AB450E",
                    "title": "4-10"
                },
                {
                    "uuid": "67EF9833-D39D-4A07-9974-6B1926474AF2",
                    "title": "asd 10"
                }
            ]
        },
        {
            "uuid": "D6F7785B-163E-4EFF-8F79-EE01F579E2A2",
            "title": "asdsda",
            "sub_questions": [
                {
                    "uuid": "8995B5A7-E698-47EF-9ADE-8107EAA13A16",
                    "title": "asdda",
                    "section_title": "dasdsa",
                    "type": "radio",
                    "answers": [
                        {
                            "uuid": "413DA1A7-2B44-4B06-9713-FB2A0020392F",
                            "title": "asda"
                        },
                        {
                            "uuid": "056ADC3A-C528-4615-9272-19EFAB73013F",
                            "title": "asdsda"
                        },
                        {
                            "uuid": "419B7C68-1032-448F-97EE-8A361605C693",
                            "title": "asdsdasda"
                        },
                        {
                            "uuid": "DC60E085-EDAB-49E1-B11A-A13C423B08B8",
                            "title": "asdsdad"
                        }
                    ]
                }

my function looks like this:-

 getNextQuestion(){
              var app = this
              // check if got_matrix = false
              if (app.got_matrix === false) {
                app.iterator = app.iterator+1
                console.log('iterator = ' + app.iterator)
                app.current_question = app.Questions[app.iterator]

               if (app.current_question.sub_questions.length > 0) { 
                  // first time matrix occurs
                  console.log('first time matrix occurs')
                  app.got_matrix = true
                  app.sub_Questions = app.current_question.sub_questions
                  console.log(app.sub_Questions)

                }

              }

              if (app.got_matrix === true) {
                // we have sub_questions
                console.log(app.sub_Questions.length)
                if (app.subQuestion_iterator+1 === app.sub_Questions.length ) {
                  // we are done iterating through all the matrix
                  console.log('no more sub_Questions')
                  app.got_matrix = false
                  app.subQuestion_iterator = 0
                  // get the next normal question
                  app.iterator = app.iterator+1;
                  app.current_question = app.Questions[app.iterator]
                }else{

                  // one more sub question 
                  console.log('one more sub question ')
                  app.subQuestion_iterator = app.subQuestion_iterator+1
                  console.log('sub_iterator = ' + app.subQuestion_iterator)
                  app.current_question = app.sub_Questions[app.subQuestion_iterator]

                }
              }

it's missing some questions, where did i mess up?

Upvotes: 2

Views: 67

Answers (1)

deefour
deefour

Reputation: 35360

How about flattening all the questions into a linear form?

const flatten = (collection, property) => (collection || []).reduce((accumulator, item) => (
    accumulator.concat(item, flatten(item[property]))
), []);


const flattenedQuestions = flatten(yourQuestions, 'sub_questions');

Now getting the next question is as simple as bumping your app.iterator:

const app = {
  iterator: 0,
};

const getNextQuestion = index => flattenedQuestions[index];


getNextQuestion(app.iterator++);
getNextQuestion(app.iterator++);
getNextQuestion(app.iterator++);

Upvotes: 1

Related Questions