skip to Main Content

I’m trying to extract values from an array of objects that are deeply nested. Below are the data and anticipated results. I’ve already produced the necessary results. If I can do it using an array, it would be great. Trying to learn a better way to achieve this and make my code look better

const questionTree=[
    {
        "question_num": 20008000,
        "parent_num": 20000000,
        "order_num": 175,
        "control_type": "S",
        "question": "PACL",
        "mask": null,
        "keyword": null,
        "description": null,
        "enable": null,
        "required": null,
        "required1": null,
        "default_value": null,
        "negative_value": null,
        "summary_string": null,
        "insert_break": null,
        "req_children_num": null,
        "urgent": null,
        "validate_expr": null,
        "validate_msg": null,
        "urgent_expr": null,
        "regex_mask": null,
        "regex_valid": null,
        "required2": null,
        "warning_msg": null,
        "linked_question_num": null,
        "state_not_valid": null,
        "count_question_num": null,
        "isQuestionAllowed": true,
        "hasDerivedValue": false,
        "answers": [
            {
                "refNum": "3977344",
                "isRequired": false,
                "isValid": true,
                "isControlDisabled": null
            }
        ],
        "children": [
            {
                "question_num": 20008050,
                "parent_num": 20008000,
                "order_num": 50,
                "control_type": "B",
                "question": "INSURANCE COMPANIES ARE REALLY PRECISE ON THIS ONE: Have you had a complete physical with your physician in the last 12 months which included a full blood workup?",
                "mask": null,
                "keyword": null,
                "description": "YesNo",
                "enable": null,
                "required": null,
                "required1": "*",
                "default_value": null,
                "negative_value": null,
                "summary_string": null,
                "insert_break": null,
                "req_children_num": null,
                "urgent": null,
                "validate_expr": null,
                "validate_msg": null,
                "urgent_expr": null,
                "regex_mask": null,
                "regex_valid": null,
                "required2": null,
                "warning_msg": null,
                "linked_question_num": null,
                "state_not_valid": null,
                "count_question_num": null,
                "isQuestionAllowed": true,
                "hasDerivedValue": false,
                "answers": [
                    {
                        "refNum": "3977344",
                        "isRequired": true,
                        "isValid": false,
                        "isControlDisabled": null
                    }
                ],
                "children": []
            },
            {
                "question_num": 20008300,
                "parent_num": 20008000,
                "order_num": 300,
                "control_type": "B",
                "question": "Within the past 5 years, have you had any judgments, collections or liens filed against you?",
                "mask": null,
                "keyword": null,
                "description": "YesNo",
                "enable": null,
                "required": null,
                "required1": "*",
                "default_value": null,
                "negative_value": null,
                "summary_string": "/",
                "insert_break": null,
                "req_children_num": 1,
                "urgent": null,
                "validate_expr": null,
                "validate_msg": null,
                "urgent_expr": null,
                "regex_mask": null,
                "regex_valid": null,
                "required2": null,
                "warning_msg": null,
                "linked_question_num": null,
                "state_not_valid": null,
                "count_question_num": null,
                "isQuestionAllowed": true,
                "hasDerivedValue": false,
                "answers": [
                    {
                        "refNum": "3977344",
                        "isRequired": true,
                        "isValid": false,
                        "isControlDisabled": false
                    }
                ],
                "children": []
            },
            {
                "question_num": 20008400,
                "parent_num": 20008000,
                "order_num": 400,
                "control_type": "B",
                "question": "In the past 5 years, have you ever requested or received a Worker's Compensation, Social Security or disability income payment, excluding a pregnancy related payment?",
                "mask": null,
                "keyword": null,
                "description": "YesNo",
                "enable": null,
                "required": null,
                "required1": "*",
                "default_value": null,
                "negative_value": null,
                "summary_string": null,
                "insert_break": null,
                "req_children_num": null,
                "urgent": null,
                "validate_expr": null,
                "validate_msg": null,
                "urgent_expr": null,
                "regex_mask": null,
                "regex_valid": null,
                "required2": null,
                "warning_msg": null,
                "linked_question_num": null,
                "state_not_valid": null,
                "count_question_num": null,
                "isQuestionAllowed": true,
                "hasDerivedValue": false,
                "answers": [
                    {
                        "refNum": "3977344",
                        "isRequired": true,
                        "isValid": false,
                        "isControlDisabled": false
                    }
                ],
                "children": []
            },
            {
                "question_num": 20008600,
                "parent_num": 20008000,
                "order_num": 600,
                "control_type": "B",
                "question": "Are you considering using funds from your existing policies or contracts to pay premiums due on the new policy or contract?",
                "mask": null,
                "keyword": null,
                "description": "YesNo",
                "enable": null,
                "required": null,
                "required1": "*",
                "default_value": null,
                "negative_value": "450",
                "summary_string": "Using funds",
                "insert_break": null,
                "req_children_num": null,
                "urgent": null,
                "validate_expr": null,
                "validate_msg": null,
                "urgent_expr": null,
                "regex_mask": null,
                "regex_valid": null,
                "required2": null,
                "warning_msg": null,
                "linked_question_num": null,
                "state_not_valid": null,
                "count_question_num": null,
                "isQuestionAllowed": true,
                "hasDerivedValue": false,
                "answers": [
                    {
                        "refNum": "3977344",
                        "isRequired": true,
                        "isValid": false,
                        "isControlDisabled": null
                    }
                ]
            },
            {
                "question_num": 20008601,
                "parent_num": 20008000,
                "order_num": 1100,
                "control_type": "S",
                "question": "Owner",
                "mask": null,
                "keyword": null,
                "description": null,
                "enable": null,
                "required": null,
                "required1": null,
                "default_value": null,
                "negative_value": null,
                "summary_string": null,
                "insert_break": 1,
                "req_children_num": null,
                "urgent": null,
                "validate_expr": null,
                "validate_msg": null,
                "urgent_expr": null,
                "regex_mask": null,
                "regex_valid": null,
                "required2": null,
                "warning_msg": null,
                "linked_question_num": null,
                "state_not_valid": null,
                "count_question_num": null,
                "isQuestionAllowed": true,
                "hasDerivedValue": false,
                "answers": [
                    {
                        "refNum": "3977344",
                        "isRequired": false,
                        "isValid": true,
                        "isControlDisabled": null
                    }
                ],
                "children": [
                    {
                        "question_num": 20008610,
                        "parent_num": 20008601,
                        "order_num": 1110,
                        "control_type": "B",
                        "question": "Are you going to be the owner of this policy?",
                        "mask": null,
                        "keyword": null,
                        "description": "YesNo",
                        "enable": null,
                        "required": null,
                        "required1": "*",
                        "default_value": null,
                        "negative_value": null,
                        "summary_string": null,
                        "insert_break": null,
                        "req_children_num": null,
                        "urgent": null,
                        "validate_expr": null,
                        "validate_msg": null,
                        "urgent_expr": null,
                        "regex_mask": null,
                        "regex_valid": null,
                        "required2": null,
                        "warning_msg": null,
                        "linked_question_num": null,
                        "state_not_valid": null,
                        "count_question_num": null,
                        "isQuestionAllowed": true,
                        "hasDerivedValue": false,
                        "answers": [
                            {
                                "refNum": "3977344",
                                "isRequired": true,
                                "isValid": false,
                                "isControlDisabled": null
                            }
                        ],
                        "children": []
                    }
                ]
            }
        ]
    },
    {
        "question_num": 20020000,
        "parent_num": 20000000,
        "order_num": 10000,
        "control_type": "S",
        "question": "Generic At Apply",
        "mask": null,
        "keyword": null,
        "description": null,
        "enable": null,
        "required": null,
        "required1": null,
        "default_value": null,
        "negative_value": null,
        "summary_string": null,
        "insert_break": null,
        "req_children_num": null,
        "urgent": null,
        "validate_expr": null,
        "validate_msg": null,
        "urgent_expr": null,
        "regex_mask": null,
        "regex_valid": null,
        "required2": null,
        "warning_msg": null,
        "linked_question_num": null,
        "state_not_valid": null,
        "count_question_num": null,
        "isQuestionAllowed": true,
        "hasDerivedValue": false,
        "answers": [
            {
                "refNum": "3977344",
                "isRequired": false,
                "isValid": true,
                "isControlDisabled": null
            }
        ],
        "children": []
    },
    {
        "question_num": 19000000,
        "parent_num": 0,
        "order_num": 1010,
        "control_type": "S",
        "question": "Purpose Of Insurance",
        "mask": null,
        "keyword": null,
        "description": null,
        "enable": null,
        "required": null,
        "required1": null,
        "default_value": null,
        "negative_value": null,
        "summary_string": null,
        "insert_break": null,
        "req_children_num": null,
        "urgent": null,
        "validate_expr": null,
        "validate_msg": null,
        "urgent_expr": null,
        "regex_mask": null,
        "regex_valid": null,
        "required2": null,
        "warning_msg": null,
        "linked_question_num": null,
        "state_not_valid": null,
        "count_question_num": null,
        "isQuestionAllowed": true,
        "hasDerivedValue": false,
        "answers": [
            {
                "refNum": "3977344",
                "isRequired": false,
                "isValid": true,
                "isControlDisabled": null
            }
        ],
        "children": [
            {
                "question_num": 19000400,
                "parent_num": 19000000,
                "order_num": 1040,
                "control_type": "C",
                "question": "Business Policy",
                "mask": null,
                "keyword": null,
                "description": null,
                "enable": null,
                "required": null,
                "required1": null,
                "default_value": null,
                "negative_value": null,
                "summary_string": "Business Policy",
                "insert_break": 1,
                "req_children_num": null,
                "urgent": null,
                "validate_expr": null,
                "validate_msg": null,
                "urgent_expr": null,
                "regex_mask": null,
                "regex_valid": null,
                "required2": null,
                "warning_msg": null,
                "linked_question_num": null,
                "state_not_valid": null,
                "count_question_num": null,
                "isQuestionAllowed": true,
                "hasDerivedValue": false,
                "answers": [
                    {
                        "refNum": "3977344",
                        "isRequired": false,
                        "isValid": true,
                        "isControlDisabled": null
                    }
                ],
                "children": []
            }
        ]
    }
]

my current code is

const getQuestionNumbersFromQuestionTree = questionTree => {
  let results = [];

  function getQuestionNum(questionTree) {
    for (let question of questionTree) {
      results.push(question.question_num);
      if (
        question.children &&
        Array.isArray(question.children) &&
        question.children.length > 0
      )
        getQuestionNum(question.children);
    }
  }

  getQuestionNum(questionTree);
  return results;
};

And it is giving desired output which is

[
  20008000, 20008050,
  20008300, 20008400,
  20008600, 20008601,
  20008610, 20020000,
  19000000, 19000400
]

just looking if I achieve same with array.reduce

2

Answers


  1. .reduce() doesn’t seem to be a good fit for this task, but here you go:

    function getQuestionNumbersFromQuestionTree(questionTree) {
      const results = [];
    
      function getQuestionNum(questionTree) {
        // .reduce() takes in and returns a reference to 'results'.
        // It runs the callback 0 times if the array it was called on has a length of 0.
        return questionTree.reduce((results, question) => {
    
          // For each question, we push its number onto 'results'
          results.push(question.question_num);
    
          // ...then go to its '.children', fallback to an array if that's nullish.
          // The callback function *must* returns the accumulated value,
          // which, in this case, is 'results', for the later call to use.
          return getQuestionNum(question.children ?? []);
        }, results);
      }
    
      return getQuestionNum(questionTree);
    }
    

    Try it:

    console.config({ maximize: true });
    
    function getQuestionNumbersFromQuestionTree(questionTree) {
      const results = [];
    
      function getQuestionNum(questionTree) {
        return questionTree.reduce((results, question) => {
          results.push(question.question_num);
          return getQuestionNum(question.children ?? []);
        }, results);
      }
    
      return getQuestionNum(questionTree);
    }
    
    const questionTree = [
      {
        question_num: 20008000,
        parent_num: 20000000,
        order_num: 175,
        control_type: 'S',
        question: 'PACL',
        mask: null,
        keyword: null,
        description: null,
        enable: null,
        required: null,
        required1: null,
        default_value: null,
        negative_value: null,
        summary_string: null,
        insert_break: null,
        req_children_num: null,
        urgent: null,
        validate_expr: null,
        validate_msg: null,
        urgent_expr: null,
        regex_mask: null,
        regex_valid: null,
        required2: null,
        warning_msg: null,
        linked_question_num: null,
        state_not_valid: null,
        count_question_num: null,
        isQuestionAllowed: true,
        hasDerivedValue: false,
        answers: [
          {
            refNum: '3977344',
            isRequired: false,
            isValid: true,
            isControlDisabled: null
          }
        ],
        children: [
          {
            question_num: 20008050,
            parent_num: 20008000,
            order_num: 50,
            control_type: 'B',
            question:
              'INSURANCE COMPANIES ARE REALLY PRECISE ON THIS ONE: Have you had a complete physical with your physician in the last 12 months which included a full blood workup?',
            mask: null,
            keyword: null,
            description: 'YesNo',
            enable: null,
            required: null,
            required1: '*',
            default_value: null,
            negative_value: null,
            summary_string: null,
            insert_break: null,
            req_children_num: null,
            urgent: null,
            validate_expr: null,
            validate_msg: null,
            urgent_expr: null,
            regex_mask: null,
            regex_valid: null,
            required2: null,
            warning_msg: null,
            linked_question_num: null,
            state_not_valid: null,
            count_question_num: null,
            isQuestionAllowed: true,
            hasDerivedValue: false,
            answers: [
              {
                refNum: '3977344',
                isRequired: true,
                isValid: false,
                isControlDisabled: null
              }
            ],
            children: []
          },
          {
            question_num: 20008300,
            parent_num: 20008000,
            order_num: 300,
            control_type: 'B',
            question: 'Within the past 5 years, have you had any judgments, collections or liens filed against you?',
            mask: null,
            keyword: null,
            description: 'YesNo',
            enable: null,
            required: null,
            required1: '*',
            default_value: null,
            negative_value: null,
            summary_string: '/',
            insert_break: null,
            req_children_num: 1,
            urgent: null,
            validate_expr: null,
            validate_msg: null,
            urgent_expr: null,
            regex_mask: null,
            regex_valid: null,
            required2: null,
            warning_msg: null,
            linked_question_num: null,
            state_not_valid: null,
            count_question_num: null,
            isQuestionAllowed: true,
            hasDerivedValue: false,
            answers: [
              {
                refNum: '3977344',
                isRequired: true,
                isValid: false,
                isControlDisabled: false
              }
            ],
            children: []
          },
          {
            question_num: 20008400,
            parent_num: 20008000,
            order_num: 400,
            control_type: 'B',
            question:
              "In the past 5 years, have you ever requested or received a Worker's Compensation, Social Security or disability income payment, excluding a pregnancy related payment?",
            mask: null,
            keyword: null,
            description: 'YesNo',
            enable: null,
            required: null,
            required1: '*',
            default_value: null,
            negative_value: null,
            summary_string: null,
            insert_break: null,
            req_children_num: null,
            urgent: null,
            validate_expr: null,
            validate_msg: null,
            urgent_expr: null,
            regex_mask: null,
            regex_valid: null,
            required2: null,
            warning_msg: null,
            linked_question_num: null,
            state_not_valid: null,
            count_question_num: null,
            isQuestionAllowed: true,
            hasDerivedValue: false,
            answers: [
              {
                refNum: '3977344',
                isRequired: true,
                isValid: false,
                isControlDisabled: false
              }
            ],
            children: []
          },
          {
            question_num: 20008600,
            parent_num: 20008000,
            order_num: 600,
            control_type: 'B',
            question:
              'Are you considering using funds from your existing policies or contracts to pay premiums due on the new policy or contract?',
            mask: null,
            keyword: null,
            description: 'YesNo',
            enable: null,
            required: null,
            required1: '*',
            default_value: null,
            negative_value: '450',
            summary_string: 'Using funds',
            insert_break: null,
            req_children_num: null,
            urgent: null,
            validate_expr: null,
            validate_msg: null,
            urgent_expr: null,
            regex_mask: null,
            regex_valid: null,
            required2: null,
            warning_msg: null,
            linked_question_num: null,
            state_not_valid: null,
            count_question_num: null,
            isQuestionAllowed: true,
            hasDerivedValue: false,
            answers: [
              {
                refNum: '3977344',
                isRequired: true,
                isValid: false,
                isControlDisabled: null
              }
            ]
          },
          {
            question_num: 20008601,
            parent_num: 20008000,
            order_num: 1100,
            control_type: 'S',
            question: 'Owner',
            mask: null,
            keyword: null,
            description: null,
            enable: null,
            required: null,
            required1: null,
            default_value: null,
            negative_value: null,
            summary_string: null,
            insert_break: 1,
            req_children_num: null,
            urgent: null,
            validate_expr: null,
            validate_msg: null,
            urgent_expr: null,
            regex_mask: null,
            regex_valid: null,
            required2: null,
            warning_msg: null,
            linked_question_num: null,
            state_not_valid: null,
            count_question_num: null,
            isQuestionAllowed: true,
            hasDerivedValue: false,
            answers: [
              {
                refNum: '3977344',
                isRequired: false,
                isValid: true,
                isControlDisabled: null
              }
            ],
            children: [
              {
                question_num: 20008610,
                parent_num: 20008601,
                order_num: 1110,
                control_type: 'B',
                question: 'Are you going to be the owner of this policy?',
                mask: null,
                keyword: null,
                description: 'YesNo',
                enable: null,
                required: null,
                required1: '*',
                default_value: null,
                negative_value: null,
                summary_string: null,
                insert_break: null,
                req_children_num: null,
                urgent: null,
                validate_expr: null,
                validate_msg: null,
                urgent_expr: null,
                regex_mask: null,
                regex_valid: null,
                required2: null,
                warning_msg: null,
                linked_question_num: null,
                state_not_valid: null,
                count_question_num: null,
                isQuestionAllowed: true,
                hasDerivedValue: false,
                answers: [
                  {
                    refNum: '3977344',
                    isRequired: true,
                    isValid: false,
                    isControlDisabled: null
                  }
                ],
                children: []
              }
            ]
          }
        ]
      },
      {
        question_num: 20020000,
        parent_num: 20000000,
        order_num: 10000,
        control_type: 'S',
        question: 'Generic At Apply',
        mask: null,
        keyword: null,
        description: null,
        enable: null,
        required: null,
        required1: null,
        default_value: null,
        negative_value: null,
        summary_string: null,
        insert_break: null,
        req_children_num: null,
        urgent: null,
        validate_expr: null,
        validate_msg: null,
        urgent_expr: null,
        regex_mask: null,
        regex_valid: null,
        required2: null,
        warning_msg: null,
        linked_question_num: null,
        state_not_valid: null,
        count_question_num: null,
        isQuestionAllowed: true,
        hasDerivedValue: false,
        answers: [
          {
            refNum: '3977344',
            isRequired: false,
            isValid: true,
            isControlDisabled: null
          }
        ],
        children: []
      },
      {
        question_num: 19000000,
        parent_num: 0,
        order_num: 1010,
        control_type: 'S',
        question: 'Purpose Of Insurance',
        mask: null,
        keyword: null,
        description: null,
        enable: null,
        required: null,
        required1: null,
        default_value: null,
        negative_value: null,
        summary_string: null,
        insert_break: null,
        req_children_num: null,
        urgent: null,
        validate_expr: null,
        validate_msg: null,
        urgent_expr: null,
        regex_mask: null,
        regex_valid: null,
        required2: null,
        warning_msg: null,
        linked_question_num: null,
        state_not_valid: null,
        count_question_num: null,
        isQuestionAllowed: true,
        hasDerivedValue: false,
        answers: [
          {
            refNum: '3977344',
            isRequired: false,
            isValid: true,
            isControlDisabled: null
          }
        ],
        children: [
          {
            question_num: 19000400,
            parent_num: 19000000,
            order_num: 1040,
            control_type: 'C',
            question: 'Business Policy',
            mask: null,
            keyword: null,
            description: null,
            enable: null,
            required: null,
            required1: null,
            default_value: null,
            negative_value: null,
            summary_string: 'Business Policy',
            insert_break: 1,
            req_children_num: null,
            urgent: null,
            validate_expr: null,
            validate_msg: null,
            urgent_expr: null,
            regex_mask: null,
            regex_valid: null,
            required2: null,
            warning_msg: null,
            linked_question_num: null,
            state_not_valid: null,
            count_question_num: null,
            isQuestionAllowed: true,
            hasDerivedValue: false,
            answers: [
              {
                refNum: '3977344',
                isRequired: false,
                isValid: true,
                isControlDisabled: null
              }
            ],
            children: []
          }
        ]
      }
    ];
    
    
    console.log(getQuestionNumbersFromQuestionTree(questionTree));
    <script src="https://gh-canon.github.io/stack-snippet-console/console.min.js"></script>

    Alternatively, you can use .forEach() for a functional style:

    function getQuestionNumbersFromQuestionTree(questionTree) {
      const results = [];
    
      // Declare a function and immediately call it
      (function getQuestionNum(questionTree) {
    
        // .forEach() doesn't return anything, so return is not needed.
        questionTree.forEach(question => {
          results.push(question.question_num);
          getQuestionNum(question.children ?? []);
        });
      })(questionTree);
      
      return results;
    }
    

    Try it:

    console.config({ maximize: true });
    
    function getQuestionNumbersFromQuestionTree(questionTree) {
      const results = [];
    
      (function getQuestionNum(questionTree) {
        questionTree.forEach(question => {
          results.push(question.question_num);
          getQuestionNum(question.children ?? []);
        });
      })(questionTree);
      
      return results;
    }
    
    const questionTree = [
      {
        question_num: 20008000,
        parent_num: 20000000,
        order_num: 175,
        control_type: 'S',
        question: 'PACL',
        mask: null,
        keyword: null,
        description: null,
        enable: null,
        required: null,
        required1: null,
        default_value: null,
        negative_value: null,
        summary_string: null,
        insert_break: null,
        req_children_num: null,
        urgent: null,
        validate_expr: null,
        validate_msg: null,
        urgent_expr: null,
        regex_mask: null,
        regex_valid: null,
        required2: null,
        warning_msg: null,
        linked_question_num: null,
        state_not_valid: null,
        count_question_num: null,
        isQuestionAllowed: true,
        hasDerivedValue: false,
        answers: [
          {
            refNum: '3977344',
            isRequired: false,
            isValid: true,
            isControlDisabled: null
          }
        ],
        children: [
          {
            question_num: 20008050,
            parent_num: 20008000,
            order_num: 50,
            control_type: 'B',
            question:
              'INSURANCE COMPANIES ARE REALLY PRECISE ON THIS ONE: Have you had a complete physical with your physician in the last 12 months which included a full blood workup?',
            mask: null,
            keyword: null,
            description: 'YesNo',
            enable: null,
            required: null,
            required1: '*',
            default_value: null,
            negative_value: null,
            summary_string: null,
            insert_break: null,
            req_children_num: null,
            urgent: null,
            validate_expr: null,
            validate_msg: null,
            urgent_expr: null,
            regex_mask: null,
            regex_valid: null,
            required2: null,
            warning_msg: null,
            linked_question_num: null,
            state_not_valid: null,
            count_question_num: null,
            isQuestionAllowed: true,
            hasDerivedValue: false,
            answers: [
              {
                refNum: '3977344',
                isRequired: true,
                isValid: false,
                isControlDisabled: null
              }
            ],
            children: []
          },
          {
            question_num: 20008300,
            parent_num: 20008000,
            order_num: 300,
            control_type: 'B',
            question: 'Within the past 5 years, have you had any judgments, collections or liens filed against you?',
            mask: null,
            keyword: null,
            description: 'YesNo',
            enable: null,
            required: null,
            required1: '*',
            default_value: null,
            negative_value: null,
            summary_string: '/',
            insert_break: null,
            req_children_num: 1,
            urgent: null,
            validate_expr: null,
            validate_msg: null,
            urgent_expr: null,
            regex_mask: null,
            regex_valid: null,
            required2: null,
            warning_msg: null,
            linked_question_num: null,
            state_not_valid: null,
            count_question_num: null,
            isQuestionAllowed: true,
            hasDerivedValue: false,
            answers: [
              {
                refNum: '3977344',
                isRequired: true,
                isValid: false,
                isControlDisabled: false
              }
            ],
            children: []
          },
          {
            question_num: 20008400,
            parent_num: 20008000,
            order_num: 400,
            control_type: 'B',
            question:
              "In the past 5 years, have you ever requested or received a Worker's Compensation, Social Security or disability income payment, excluding a pregnancy related payment?",
            mask: null,
            keyword: null,
            description: 'YesNo',
            enable: null,
            required: null,
            required1: '*',
            default_value: null,
            negative_value: null,
            summary_string: null,
            insert_break: null,
            req_children_num: null,
            urgent: null,
            validate_expr: null,
            validate_msg: null,
            urgent_expr: null,
            regex_mask: null,
            regex_valid: null,
            required2: null,
            warning_msg: null,
            linked_question_num: null,
            state_not_valid: null,
            count_question_num: null,
            isQuestionAllowed: true,
            hasDerivedValue: false,
            answers: [
              {
                refNum: '3977344',
                isRequired: true,
                isValid: false,
                isControlDisabled: false
              }
            ],
            children: []
          },
          {
            question_num: 20008600,
            parent_num: 20008000,
            order_num: 600,
            control_type: 'B',
            question:
              'Are you considering using funds from your existing policies or contracts to pay premiums due on the new policy or contract?',
            mask: null,
            keyword: null,
            description: 'YesNo',
            enable: null,
            required: null,
            required1: '*',
            default_value: null,
            negative_value: '450',
            summary_string: 'Using funds',
            insert_break: null,
            req_children_num: null,
            urgent: null,
            validate_expr: null,
            validate_msg: null,
            urgent_expr: null,
            regex_mask: null,
            regex_valid: null,
            required2: null,
            warning_msg: null,
            linked_question_num: null,
            state_not_valid: null,
            count_question_num: null,
            isQuestionAllowed: true,
            hasDerivedValue: false,
            answers: [
              {
                refNum: '3977344',
                isRequired: true,
                isValid: false,
                isControlDisabled: null
              }
            ]
          },
          {
            question_num: 20008601,
            parent_num: 20008000,
            order_num: 1100,
            control_type: 'S',
            question: 'Owner',
            mask: null,
            keyword: null,
            description: null,
            enable: null,
            required: null,
            required1: null,
            default_value: null,
            negative_value: null,
            summary_string: null,
            insert_break: 1,
            req_children_num: null,
            urgent: null,
            validate_expr: null,
            validate_msg: null,
            urgent_expr: null,
            regex_mask: null,
            regex_valid: null,
            required2: null,
            warning_msg: null,
            linked_question_num: null,
            state_not_valid: null,
            count_question_num: null,
            isQuestionAllowed: true,
            hasDerivedValue: false,
            answers: [
              {
                refNum: '3977344',
                isRequired: false,
                isValid: true,
                isControlDisabled: null
              }
            ],
            children: [
              {
                question_num: 20008610,
                parent_num: 20008601,
                order_num: 1110,
                control_type: 'B',
                question: 'Are you going to be the owner of this policy?',
                mask: null,
                keyword: null,
                description: 'YesNo',
                enable: null,
                required: null,
                required1: '*',
                default_value: null,
                negative_value: null,
                summary_string: null,
                insert_break: null,
                req_children_num: null,
                urgent: null,
                validate_expr: null,
                validate_msg: null,
                urgent_expr: null,
                regex_mask: null,
                regex_valid: null,
                required2: null,
                warning_msg: null,
                linked_question_num: null,
                state_not_valid: null,
                count_question_num: null,
                isQuestionAllowed: true,
                hasDerivedValue: false,
                answers: [
                  {
                    refNum: '3977344',
                    isRequired: true,
                    isValid: false,
                    isControlDisabled: null
                  }
                ],
                children: []
              }
            ]
          }
        ]
      },
      {
        question_num: 20020000,
        parent_num: 20000000,
        order_num: 10000,
        control_type: 'S',
        question: 'Generic At Apply',
        mask: null,
        keyword: null,
        description: null,
        enable: null,
        required: null,
        required1: null,
        default_value: null,
        negative_value: null,
        summary_string: null,
        insert_break: null,
        req_children_num: null,
        urgent: null,
        validate_expr: null,
        validate_msg: null,
        urgent_expr: null,
        regex_mask: null,
        regex_valid: null,
        required2: null,
        warning_msg: null,
        linked_question_num: null,
        state_not_valid: null,
        count_question_num: null,
        isQuestionAllowed: true,
        hasDerivedValue: false,
        answers: [
          {
            refNum: '3977344',
            isRequired: false,
            isValid: true,
            isControlDisabled: null
          }
        ],
        children: []
      },
      {
        question_num: 19000000,
        parent_num: 0,
        order_num: 1010,
        control_type: 'S',
        question: 'Purpose Of Insurance',
        mask: null,
        keyword: null,
        description: null,
        enable: null,
        required: null,
        required1: null,
        default_value: null,
        negative_value: null,
        summary_string: null,
        insert_break: null,
        req_children_num: null,
        urgent: null,
        validate_expr: null,
        validate_msg: null,
        urgent_expr: null,
        regex_mask: null,
        regex_valid: null,
        required2: null,
        warning_msg: null,
        linked_question_num: null,
        state_not_valid: null,
        count_question_num: null,
        isQuestionAllowed: true,
        hasDerivedValue: false,
        answers: [
          {
            refNum: '3977344',
            isRequired: false,
            isValid: true,
            isControlDisabled: null
          }
        ],
        children: [
          {
            question_num: 19000400,
            parent_num: 19000000,
            order_num: 1040,
            control_type: 'C',
            question: 'Business Policy',
            mask: null,
            keyword: null,
            description: null,
            enable: null,
            required: null,
            required1: null,
            default_value: null,
            negative_value: null,
            summary_string: 'Business Policy',
            insert_break: 1,
            req_children_num: null,
            urgent: null,
            validate_expr: null,
            validate_msg: null,
            urgent_expr: null,
            regex_mask: null,
            regex_valid: null,
            required2: null,
            warning_msg: null,
            linked_question_num: null,
            state_not_valid: null,
            count_question_num: null,
            isQuestionAllowed: true,
            hasDerivedValue: false,
            answers: [
              {
                refNum: '3977344',
                isRequired: false,
                isValid: true,
                isControlDisabled: null
              }
            ],
            children: []
          }
        ]
      }
    ];
    
    
    console.log(getQuestionNumbersFromQuestionTree(questionTree));
    <script src="https://gh-canon.github.io/stack-snippet-console/console.min.js"></script>

    Both of these are not as clear (and, perhaps, as fast) as your original solution, so my recommendation is that you should just use yours.

    Login or Signup to reply.
  2. As already noted in the comments, your for...of loop is clearer and probably more performant than an equivalent reduce(). There is no need for the nested function though, you can simply recurse on the outer function.

    const questionTree = [{ question_num: 20008000, parent_num: 20000000, order_num: 175, control_type: 'S', question: 'PACL', mask: null, keyword: null, description: null, enable: null, required: null, required1: null, default_value: null, negative_value: null, summary_string: null, insert_break: null, req_children_num: null, urgent: null, validate_expr: null, validate_msg: null, urgent_expr: null, regex_mask: null, regex_valid: null, required2: null, warning_msg: null, linked_question_num: null, state_not_valid: null, count_question_num: null, isQuestionAllowed: true, hasDerivedValue: false, answers: [{ refNum: '3977344', isRequired: false, isValid: true, isControlDisabled: null, },], children: [{ question_num: 20008050, parent_num: 20008000, order_num: 50, control_type: 'B', question: 'INSURANCE COMPANIES ARE REALLY PRECISE ON THIS ONE: Have you had a complete physical with your physician in the last 12 months which included a full blood workup?', mask: null, keyword: null, description: 'YesNo', enable: null, required: null, required1: '*', default_value: null, negative_value: null, summary_string: null, insert_break: null, req_children_num: null, urgent: null, validate_expr: null, validate_msg: null, urgent_expr: null, regex_mask: null, regex_valid: null, required2: null, warning_msg: null, linked_question_num: null, state_not_valid: null, count_question_num: null, isQuestionAllowed: true, hasDerivedValue: false, answers: [{ refNum: '3977344', isRequired: true, isValid: false, isControlDisabled: null, },], children: [], }, { question_num: 20008300, parent_num: 20008000, order_num: 300, control_type: 'B', question: 'Within the past 5 years, have you had any judgments, collections or liens filed against you?', mask: null, keyword: null, description: 'YesNo', enable: null, required: null, required1: '*', default_value: null, negative_value: null, summary_string: '/', insert_break: null, req_children_num: 1, urgent: null, validate_expr: null, validate_msg: null, urgent_expr: null, regex_mask: null, regex_valid: null, required2: null, warning_msg: null, linked_question_num: null, state_not_valid: null, count_question_num: null, isQuestionAllowed: true, hasDerivedValue: false, answers: [{ refNum: '3977344', isRequired: true, isValid: false, isControlDisabled: false, },], children: [], }, { question_num: 20008400, parent_num: 20008000, order_num: 400, control_type: 'B', question: "In the past 5 years, have you ever requested or received a Worker's Compensation, Social Security or disability income payment, excluding a pregnancy related payment?", mask: null, keyword: null, description: 'YesNo', enable: null, required: null, required1: '*', default_value: null, negative_value: null, summary_string: null, insert_break: null, req_children_num: null, urgent: null, validate_expr: null, validate_msg: null, urgent_expr: null, regex_mask: null, regex_valid: null, required2: null, warning_msg: null, linked_question_num: null, state_not_valid: null, count_question_num: null, isQuestionAllowed: true, hasDerivedValue: false, answers: [{ refNum: '3977344', isRequired: true, isValid: false, isControlDisabled: false, },], children: [], }, { question_num: 20008600, parent_num: 20008000, order_num: 600, control_type: 'B', question: 'Are you considering using funds from your existing policies or contracts to pay premiums due on the new policy or contract?', mask: null, keyword: null, description: 'YesNo', enable: null, required: null, required1: '*', default_value: null, negative_value: '450', summary_string: 'Using funds', insert_break: null, req_children_num: null, urgent: null, validate_expr: null, validate_msg: null, urgent_expr: null, regex_mask: null, regex_valid: null, required2: null, warning_msg: null, linked_question_num: null, state_not_valid: null, count_question_num: null, isQuestionAllowed: true, hasDerivedValue: false, answers: [{ refNum: '3977344', isRequired: true, isValid: false, isControlDisabled: null, },], }, { question_num: 20008601, parent_num: 20008000, order_num: 1100, control_type: 'S', question: 'Owner', mask: null, keyword: null, description: null, enable: null, required: null, required1: null, default_value: null, negative_value: null, summary_string: null, insert_break: 1, req_children_num: null, urgent: null, validate_expr: null, validate_msg: null, urgent_expr: null, regex_mask: null, regex_valid: null, required2: null, warning_msg: null, linked_question_num: null, state_not_valid: null, count_question_num: null, isQuestionAllowed: true, hasDerivedValue: false, answers: [{ refNum: '3977344', isRequired: false, isValid: true, isControlDisabled: null, },], children: [{ question_num: 20008610, parent_num: 20008601, order_num: 1110, control_type: 'B', question: 'Are you going to be the owner of this policy?', mask: null, keyword: null, description: 'YesNo', enable: null, required: null, required1: '*', default_value: null, negative_value: null, summary_string: null, insert_break: null, req_children_num: null, urgent: null, validate_expr: null, validate_msg: null, urgent_expr: null, regex_mask: null, regex_valid: null, required2: null, warning_msg: null, linked_question_num: null, state_not_valid: null, count_question_num: null, isQuestionAllowed: true, hasDerivedValue: false, answers: [{ refNum: '3977344', isRequired: true, isValid: false, isControlDisabled: null, },], children: [], },], },], }, { question_num: 20020000, parent_num: 20000000, order_num: 10000, control_type: 'S', question: 'Generic At Apply', mask: null, keyword: null, description: null, enable: null, required: null, required1: null, default_value: null, negative_value: null, summary_string: null, insert_break: null, req_children_num: null, urgent: null, validate_expr: null, validate_msg: null, urgent_expr: null, regex_mask: null, regex_valid: null, required2: null, warning_msg: null, linked_question_num: null, state_not_valid: null, count_question_num: null, isQuestionAllowed: true, hasDerivedValue: false, answers: [{ refNum: '3977344', isRequired: false, isValid: true, isControlDisabled: null, },], children: [], }, { question_num: 19000000, parent_num: 0, order_num: 1010, control_type: 'S', question: 'Purpose Of Insurance', mask: null, keyword: null, description: null, enable: null, required: null, required1: null, default_value: null, negative_value: null, summary_string: null, insert_break: null, req_children_num: null, urgent: null, validate_expr: null, validate_msg: null, urgent_expr: null, regex_mask: null, regex_valid: null, required2: null, warning_msg: null, linked_question_num: null, state_not_valid: null, count_question_num: null, isQuestionAllowed: true, hasDerivedValue: false, answers: [{ refNum: '3977344', isRequired: false, isValid: true, isControlDisabled: null, },], children: [{ question_num: 19000400, parent_num: 19000000, order_num: 1040, control_type: 'C', question: 'Business Policy', mask: null, keyword: null, description: null, enable: null, required: null, required1: null, default_value: null, negative_value: null, summary_string: 'Business Policy', insert_break: 1, req_children_num: null, urgent: null, validate_expr: null, validate_msg: null, urgent_expr: null, regex_mask: null, regex_valid: null, required2: null, warning_msg: null, linked_question_num: null, state_not_valid: null, count_question_num: null, isQuestionAllowed: true, hasDerivedValue: false, answers: [{ refNum: '3977344', isRequired: false, isValid: true, isControlDisabled: null, },], children: [], },], },];
    
    function getQuestionNumbersFromQuestionTree(questionTree) {
      let results = [];
    
      for (const question of questionTree) {
        results.push(
          question.question_num,
          ...getQuestionNumbersFromQuestionTree(question.children ?? [])
        );
      }
    
      return results;
    }
    
    console.log(getQuestionNumbersFromQuestionTree(questionTree));

    If you do want to use an Array method then flatMap() is probably the most directly applicable.

    const questionTree = [{ question_num: 20008000, parent_num: 20000000, order_num: 175, control_type: 'S', question: 'PACL', mask: null, keyword: null, description: null, enable: null, required: null, required1: null, default_value: null, negative_value: null, summary_string: null, insert_break: null, req_children_num: null, urgent: null, validate_expr: null, validate_msg: null, urgent_expr: null, regex_mask: null, regex_valid: null, required2: null, warning_msg: null, linked_question_num: null, state_not_valid: null, count_question_num: null, isQuestionAllowed: true, hasDerivedValue: false, answers: [{ refNum: '3977344', isRequired: false, isValid: true, isControlDisabled: null, },], children: [{ question_num: 20008050, parent_num: 20008000, order_num: 50, control_type: 'B', question: 'INSURANCE COMPANIES ARE REALLY PRECISE ON THIS ONE: Have you had a complete physical with your physician in the last 12 months which included a full blood workup?', mask: null, keyword: null, description: 'YesNo', enable: null, required: null, required1: '*', default_value: null, negative_value: null, summary_string: null, insert_break: null, req_children_num: null, urgent: null, validate_expr: null, validate_msg: null, urgent_expr: null, regex_mask: null, regex_valid: null, required2: null, warning_msg: null, linked_question_num: null, state_not_valid: null, count_question_num: null, isQuestionAllowed: true, hasDerivedValue: false, answers: [{ refNum: '3977344', isRequired: true, isValid: false, isControlDisabled: null, },], children: [], }, { question_num: 20008300, parent_num: 20008000, order_num: 300, control_type: 'B', question: 'Within the past 5 years, have you had any judgments, collections or liens filed against you?', mask: null, keyword: null, description: 'YesNo', enable: null, required: null, required1: '*', default_value: null, negative_value: null, summary_string: '/', insert_break: null, req_children_num: 1, urgent: null, validate_expr: null, validate_msg: null, urgent_expr: null, regex_mask: null, regex_valid: null, required2: null, warning_msg: null, linked_question_num: null, state_not_valid: null, count_question_num: null, isQuestionAllowed: true, hasDerivedValue: false, answers: [{ refNum: '3977344', isRequired: true, isValid: false, isControlDisabled: false, },], children: [], }, { question_num: 20008400, parent_num: 20008000, order_num: 400, control_type: 'B', question: "In the past 5 years, have you ever requested or received a Worker's Compensation, Social Security or disability income payment, excluding a pregnancy related payment?", mask: null, keyword: null, description: 'YesNo', enable: null, required: null, required1: '*', default_value: null, negative_value: null, summary_string: null, insert_break: null, req_children_num: null, urgent: null, validate_expr: null, validate_msg: null, urgent_expr: null, regex_mask: null, regex_valid: null, required2: null, warning_msg: null, linked_question_num: null, state_not_valid: null, count_question_num: null, isQuestionAllowed: true, hasDerivedValue: false, answers: [{ refNum: '3977344', isRequired: true, isValid: false, isControlDisabled: false, },], children: [], }, { question_num: 20008600, parent_num: 20008000, order_num: 600, control_type: 'B', question: 'Are you considering using funds from your existing policies or contracts to pay premiums due on the new policy or contract?', mask: null, keyword: null, description: 'YesNo', enable: null, required: null, required1: '*', default_value: null, negative_value: '450', summary_string: 'Using funds', insert_break: null, req_children_num: null, urgent: null, validate_expr: null, validate_msg: null, urgent_expr: null, regex_mask: null, regex_valid: null, required2: null, warning_msg: null, linked_question_num: null, state_not_valid: null, count_question_num: null, isQuestionAllowed: true, hasDerivedValue: false, answers: [{ refNum: '3977344', isRequired: true, isValid: false, isControlDisabled: null, },], }, { question_num: 20008601, parent_num: 20008000, order_num: 1100, control_type: 'S', question: 'Owner', mask: null, keyword: null, description: null, enable: null, required: null, required1: null, default_value: null, negative_value: null, summary_string: null, insert_break: 1, req_children_num: null, urgent: null, validate_expr: null, validate_msg: null, urgent_expr: null, regex_mask: null, regex_valid: null, required2: null, warning_msg: null, linked_question_num: null, state_not_valid: null, count_question_num: null, isQuestionAllowed: true, hasDerivedValue: false, answers: [{ refNum: '3977344', isRequired: false, isValid: true, isControlDisabled: null, },], children: [{ question_num: 20008610, parent_num: 20008601, order_num: 1110, control_type: 'B', question: 'Are you going to be the owner of this policy?', mask: null, keyword: null, description: 'YesNo', enable: null, required: null, required1: '*', default_value: null, negative_value: null, summary_string: null, insert_break: null, req_children_num: null, urgent: null, validate_expr: null, validate_msg: null, urgent_expr: null, regex_mask: null, regex_valid: null, required2: null, warning_msg: null, linked_question_num: null, state_not_valid: null, count_question_num: null, isQuestionAllowed: true, hasDerivedValue: false, answers: [{ refNum: '3977344', isRequired: true, isValid: false, isControlDisabled: null, },], children: [], },], },], }, { question_num: 20020000, parent_num: 20000000, order_num: 10000, control_type: 'S', question: 'Generic At Apply', mask: null, keyword: null, description: null, enable: null, required: null, required1: null, default_value: null, negative_value: null, summary_string: null, insert_break: null, req_children_num: null, urgent: null, validate_expr: null, validate_msg: null, urgent_expr: null, regex_mask: null, regex_valid: null, required2: null, warning_msg: null, linked_question_num: null, state_not_valid: null, count_question_num: null, isQuestionAllowed: true, hasDerivedValue: false, answers: [{ refNum: '3977344', isRequired: false, isValid: true, isControlDisabled: null, },], children: [], }, { question_num: 19000000, parent_num: 0, order_num: 1010, control_type: 'S', question: 'Purpose Of Insurance', mask: null, keyword: null, description: null, enable: null, required: null, required1: null, default_value: null, negative_value: null, summary_string: null, insert_break: null, req_children_num: null, urgent: null, validate_expr: null, validate_msg: null, urgent_expr: null, regex_mask: null, regex_valid: null, required2: null, warning_msg: null, linked_question_num: null, state_not_valid: null, count_question_num: null, isQuestionAllowed: true, hasDerivedValue: false, answers: [{ refNum: '3977344', isRequired: false, isValid: true, isControlDisabled: null, },], children: [{ question_num: 19000400, parent_num: 19000000, order_num: 1040, control_type: 'C', question: 'Business Policy', mask: null, keyword: null, description: null, enable: null, required: null, required1: null, default_value: null, negative_value: null, summary_string: 'Business Policy', insert_break: 1, req_children_num: null, urgent: null, validate_expr: null, validate_msg: null, urgent_expr: null, regex_mask: null, regex_valid: null, required2: null, warning_msg: null, linked_question_num: null, state_not_valid: null, count_question_num: null, isQuestionAllowed: true, hasDerivedValue: false, answers: [{ refNum: '3977344', isRequired: false, isValid: true, isControlDisabled: null, },], children: [], },], },];
    
    const getQuestionNumbersFromQuestionTree = (questionTree) =>
      questionTree.flatMap((question) => [
        question.question_num,
        ...getQuestionNumbersFromQuestionTree(question.children ?? []),
      ]);
    
    console.log(getQuestionNumbersFromQuestionTree(questionTree));
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search