Home > Software design >  Array formations
Array formations

Time:02-06

Here i have a array of objects. I want the each grade as seperate array and inside the array i want the grade based section and subjects.

If teacher is taking a grade i want the schema as which grade they take and what section, inside that what subject they are taking.

var user = [
  RowDataPacket { grade_id: 3, subject_id: 3, section_id: 747 },
  RowDataPacket { grade_id: 4, subject_id: 3, section_id: 748 },
  RowDataPacket { grade_id: 5, subject_id: 3, section_id: 749 },
  RowDataPacket { grade_id: 6, subject_id: 3, section_id: 750 },
  RowDataPacket { grade_id: 7, subject_id: 3, section_id: 751 },
  RowDataPacket { grade_id: 8, subject_id: 3, section_id: 752 },
  RowDataPacket { grade_id: 7, subject_id: 4, section_id: 751 }
]

Schema required:

  "gradeDetails"  [{
    grade_id:"3"
    section_details:[{
      section_id:"",
      subjectDetails:[{
        subject_id:""},
        {subject_id:""}
        ]
       }
      ],
       grade_id:"4"
       section_details:[{
       section_id:"",
        subjectDetails:[{
        subject_id:""},
        {subject_id:""}
        ]
       },
       {
       section_id:"",
        subjectDetails:[{
        subject_id:""},
        {subject_id:""}
        ]
       }
      ],
  }
    ]

CodePudding user response:

The below code snippet may be one way to achieve the desired result:

Code Sample

const getGradeDetailsArray = (arr = user) => {
  const resultArr = [];
  for (const grade of (new Set(arr.map(obj => obj.grade_id)))) {
    const resObj = {
        grade_id: grade,
      section_details: arr.filter(
        obj => obj.grade_id === grade
        ).map(
        obj => ({
            section_id: obj.section_id,
          subject_details: [{ subject_id: obj.subject_id }]
          })
        )
    };
    resultArr.push(resObj)
  };
  return resultArr;
};

Explanation

  • Initialize the result as an empty-array named resultArr
  • Obtain unique grade_ids by using new Set() and iterate over each grade
  • For each grade, construct an object to be pushed to the result
  • grade_id is set to grade
  • section_details is set as an array filtered by matching grade_id
  • subect_details is an array with exactly one element (ie, the subject_id).
  • Return the resultArr

Code Snippet

const user = [
    { grade_id: 3, subject_id: 3, section_id: 747 },
  { grade_id: 4, subject_id: 3, section_id: 748 },
  { grade_id: 5, subject_id: 3, section_id: 749 },
  { grade_id: 6, subject_id: 3, section_id: 750 },
  { grade_id: 7, subject_id: 3, section_id: 751 },
  { grade_id: 8, subject_id: 3, section_id: 752 },
  { grade_id: 7, subject_id: 4, section_id: 751 },
];

const getGradeDetailsArray = (arr = user) => {
  const resultArr = [];
  for (const grade of (new Set(arr.map(obj => obj.grade_id)))) {
    const resObj = {
        grade_id: grade,
      section_details: arr.filter(
        obj => obj.grade_id === grade
        ).map(
        obj => ({
            section_id: obj.section_id,
          subject_details: [{ subject_id: obj.subject_id }]
          })
        )
    };
    resultArr.push(resObj)
  };
  return resultArr;
};

console.log(getGradeDetailsArray());

  •  Tags:  
  • Related