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
.reduce()
doesn’t seem to be a good fit for this task, but here you go:Try it:
Alternatively, you can use
.forEach()
for a functional style:Try it:
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.
As already noted in the comments, your
for...of
loop is clearer and probably more performant than an equivalentreduce()
. There is no need for the nested function though, you can simply recurse on the outer function.If you do want to use an Array method then
flatMap()
is probably the most directly applicable.