Home > Software engineering >  Nodejs how to create a `data tree` on result
Nodejs how to create a `data tree` on result

Time:09-12

How to fix or redesign results or create a TreeView after mapping the result using nodejs? this is my code, please check below the desired result

const getquestionnaire = await QuestionnaireModels.getQuestion(yearlevel, section);
res.send(getquestionnaire);

getQuestion

async getQuestion(yearlevel, section) {
    let student = await Database.getConnection().models.student;
    let qgroup = await Database.getConnection().models.questiongroups;
    let qdetails = await Database.getConnection().models.questiondetails;
    let oitem = await Database.getConnection().models.optionitems;
    student.hasMany(qgroup, { foreignKey: '_id'});
    student.hasMany(qdetails, {foreignKey: 'QuestionGroupID'})
    student.hasMany(oitem, {foreignKey: 'QuestionDetailsID'})
    let question = await Database.getConnection().models.student.findAll({
     attributes:[
    'yearlevel','section','questiongroups.GroupName','questiondetails.QuestionDescription','optionitems.OptionDescription'
    ],
      where: {
        yearlevel: yearlevel,
        section: section
      },
      include:[
        { model: qgroup, attribute:[] },
        { model: qdetails, attribute:[] },
        { model: oitem, attribute:[] },
    ],
      raw: true,
    })
    return result
  },
  

current result

{
    "yearlevel": "Grade 7",
    "section": "Apple",
    "Groupname": "Third",
    "QuestionDescription":"Eating Disorder",
    "OptionDescription": "Test"
        
},
{
    "yearlevel": "Grade 8",
    "section": "Blue",
    "Groupname": "Test Group name",
    "QuestionDescription":"Test Question",
    "OptionDescription": "Test"
        
}

desired result

[
    {
        "yearlevel": "Grade 7",
        "section": "Apple",
        "question":{
            "Groupname": "Third",
            "groupdetail":{
                "QuestionDescription":"Eating Disorder",
                "optionItems":{
                    "OptionDescription": "Test"
                }
            }
        }
    },
    {
        "yearlevel": "Grade 8",
        "section": "Blue",
        "question":{
            "Groupname": "Test Group name",
            "groupdetail":{
                "QuestionDescription":"Test Question",
                "optionItems":{
                    "OptionDescription": "Test"
                }
            }
        }
    }
]

dont mind this message dont mind this message dont mind this message dont mind this message dont mind this message dont mind this message dont mind this message dont mind this message dont mind this message

CodePudding user response:

const currentResult = [{
   "yearlevel": "Grade 7",
   "section": "Apple",
   "Groupname": "Third",
   "QuestionDescription": "Eating Disorder",
   "OptionDescription": "Test"

},
{
   "yearlevel": "Grade 8",
   "section": "Blue",
   "Groupname": "Test Group name",
   "QuestionDescription": "Test Question",
   "OptionDescription": "Test"

}];

const desiredResult = currentResult.map(({ yearlevel, section, Groupname, QuestionDescription, OptionDescription }) => ({
   yearlevel,
   section,
   question: {
      Groupname,
      groupdetails: {
         QuestionDescription,
         optionItems: {
            OptionDescription
         }
      }
   }
}))
  • Related