遍历多级数组

时间:2018-08-29 20:18:11

标签: javascript

我有一个要迭代的问题,每个问题都可能有子问题,我想遍历所有这些问题: 我的变量是:-

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

这些是我的数据对象: 这是我的功能,当我提交值时,我想获得下一个问题,并检查它是否包含子问题,如果存在,则遍历子问题,当它们完成后返回主要问题,这就是json数据如下:-

 {

            "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"
                        }
                    ]
                }

我的功能如下:-

 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]

                }
              }

它遗漏了一些问题,我在哪里弄糟了?

1 个答案:

答案 0 :(得分:1)

如何将所有问题展平为线性形式?

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


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

现在得到下一个问题就像颠倒app.iterator一样简单:

const app = {
  iterator: 0,
};

const getNextQuestion = index => flattenedQuestions[index];


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