Within each subQuestion
object, I have an array of questions that I need to extract and add to the parent level array, which is the subQuestion
. The current code doesn't seem to be achieving this. Is there anything wrong with the following code snippet?
main.ts
if (_.isArray(_answerOption.subQuestion)) {
for (let iLoop = 0, ll = _answerOption.subQuestion.length; iLoop < ll; iLoop++) {
// Replace subQuestions entirely with the content of the question
if (_answerOption.subQuestion && _answerOption.subQuestion[iLoop].question) {
_answerOption.subQuestion = _.cloneDeep(_answerOption.subQuestion[iLoop].question);
}
}
}
data
{
"nonClinicalIndicator": "Y",
"questionId": 2558,
"questionId2": 116523,
"questionText": "How much of your medication(s) do you have left? For insurance purposes you must provide exact number of pills, injections, doses etc",
"answerId": 0,
"answerType": "SINGLE_SELECT",
"responseFieldIdentifier": "DOSE LEFT IND",
"answerOption": [
{
"answerOptionId": 2559,
"answerOptionId2": 116524,
"answerText": "Yes",
"subQuestion": [
{
"question": [
{
"nonClinicalIndicator": "Y",
"questionId": 2560,
"questionId2": 116525,
"questionText": "Number of doses left",
"answerId": 0,
"answerType": "TEXT",
"responseFieldIdentifier": "DOSE LEFT"
},
{
"nonClinicalIndicator": "Y",
"questionId": 2561,
"questionId2": 116526,
"questionText": "When will you take your next dose?",
"answerId": 0,
"answerType": "TEXT_DATE",
"responseFieldIdentifier": "NEXT DOSE"
}
],
"rxNumber": "15127724",
"drugName": "TIKOSYN 250MCG CAPS",
"drugNdc": "00069581060"
},
{
"question": [
{
"nonClinicalIndicator": "Y",
"questionId": 2560,
"questionId2": 116525,
"questionText": "Number of doses left",
"answerId": 0,
"answerType": "TEXT",
"responseFieldIdentifier": "DOSE LEFT"
},
{
"nonClinicalIndicator": "Y",
"questionId": 2561,
"questionId2": 116526,
"questionText": "When will you take your next dose?",
"answerId": 0,
"answerType": "TEXT_DATE",
"responseFieldIdentifier": "NEXT DOSE"
}
],
"rxNumber": "15127730",
"drugName": "TACROLIMUS 1MG CAPS",
"drugNdc": "55111052601"
}
]
},
{
"answerOptionId": 2562,
"answerOptionId2": 116527,
"answerText": "No"
}
]
}
Expected Output
{
"nonClinicalIndicator": "Y",
"questionId": 2558,
"questionId2": 116523,
"questionText": "How much of your medication(s) do you have left? For insurance purposes you must provide exact number of pills, injections, doses etc",
"answerId": 0,
"answerType": "SINGLE_SELECT",
"responseFieldIdentifier": "DOSE LEFT IND",
"answerOption": [
{
"answerOptionId": 2559,
"answerOptionId2": 116524,
"answerText": "Yes",
"subQuestion": [
{
"nonClinicalIndicator": "Y",
"questionId": 2560,
"questionId2": 116525,
"questionText": "Number of doses left",
"answerId": 0,
"answerType": "TEXT",
"responseFieldIdentifier": "DOSE LEFT",
"answerOption": [
{
"answerOptionId": 0,
"answerText": "DRUG 1one dose",
"answerOptionId2": 0
}
]
},
{
"nonClinicalIndicator": "Y",
"questionId": 2561,
"questionId2": 116526,
"questionText": "When will you take your next dose?",
"answerId": 0,
"answerType": "TEXT_DATE",
"responseFieldIdentifier": "NEXT DOSE",
"answerOption": [
{
"answerOptionId": 0,
"answerText": "2019-03-28",
"answerOptionId2": 0
}
]
},
{
"nonClinicalIndicator": "Y",
"questionId": 2560,
"questionId2": 116525,
"questionText": "Number of doses left",
"answerId": 0,
"answerType": "TEXT",
"responseFieldIdentifier": "DOSE LEFT",
"answerOption": [
{
"answerOptionId": 0,
"answerText": "DRUG 2 one dose",
"answerOptionId2": 0
}
]
},
{
"nonClinicalIndicator": "Y",
"questionId": 2561,
"questionId2": 116526,
"questionText": "When will you take your next dose?",
"answerId": 0,
"answerType": "TEXT_DATE",
"responseFieldIdentifier": "NEXT DOSE",
"answerOption": [
{
"answerOptionId": 0,
"answerText": "2019-03-31",
"answerOptionId2": 0
}
]
}
]
},
{
"answerOptionId": 2562,
"answerOptionId2": 116527,
"answerText": "No"
}
]
}