我想在每个subQuestion
对象中都有一个问题数组,我想获取每个问题数组的元素并将其推送到subQuestion
的父级数组中。以下代码不会发生以下代码有什么问题吗?
main.ts
if (_.isArray(_answerOption.subQuestion)) {
for (let iLoop = 0, ll = _answerOption.subQuestion.length; iLoop < ll; iLoop++) {
// compeltely replace the subQuestions with the contents on the question
if (_answerOption.subQuestion && _answerOption.subQuestion[iLoop].question) {
_answerOption.subQuestion = _.cloneDeep(_answerOption.subQuestion[iLoop].question);
}
}
}
数据
{
"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"
}
]
}
预期产量
{
"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"
}
]
}
答案 0 :(得分:0)
您可以尝试以下操作:
const 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"
}
]
};
// Destructure answerOption property first as we gonna focus on it
const { answerOption, ...rest} = data;
const newAnswerOptions = [];
for (let i = 0; i < answerOption.length; i++) {
// If the answerOption property has a key subQuestion which is not undefined
if (answerOption[i].subQuestion) {
const { subQuestion, ...restAnswers } = answerOption[i];
let questions = subQuestion.map(x => x.question).flat();
newAnswerOptions.push(Object.assign({}, restAnswers, { subQuestion: questions }));
}
// Else simply push it
else {
newAnswerOptions.push(answerOption[i]);
}
}
const res = { ...rest, answerOption: newAnswerOptions };
console.log(JSON.stringify(res, null, 4));