'Set (Group) grade array with nested section array and section contains subject array

Here I have an array of objects. I want each grade as a separate array and inside the array, I want the grade based section and subjects.

The schema is with a set of grades, in each grade contains a section array; and in each section contains the subject array.

Input (Sample from DB):

[
  RowDataPacket {
    grade_id: 4,
    grade_name: '4',
    subject_id: 3,
    subject_name: 'Maths',
    section_id: 209,
    section_name: 'A'
  },
  RowDataPacket {
    grade_id: 4,
    grade_name: '4',
    subject_id: 3,
    subject_name: 'Maths',
    section_id: 924,
    section_name: 'B'
  },
  RowDataPacket {
    grade_id: 5,
    grade_name: '5',
    subject_id: 3,
    subject_name: 'Maths',
    section_id: 210,
    section_name: 'A'
  },
  RowDataPacket {
    grade_id: 3,
    grade_name: '3',
    subject_id: 3,
    subject_name: 'Maths',
    section_id: 208,
    section_name: 'A'
  },
  RowDataPacket {
    grade_id: 3,
    grade_name: '3',
    subject_id: 3,
    subject_name: 'Maths',
    section_id: 727,
    section_name: 'B'
  },
  RowDataPacket {
    grade_id: 4,
    grade_name: '4',
    subject_id: 2,
    subject_name: 'English',
    section_id: 209,
    section_name: 'A'
  },
  RowDataPacket {
    grade_id: 4,
    grade_name: '4',
    subject_id: 4,
    subject_name: 'Science',
    section_id: 209,
    section_name: 'A'
  },
  RowDataPacket {
    grade_id: 4,
    grade_name: '4',
    subject_id: 5,
    subject_name: 'EVS',
    section_id: 209,
    section_name: 'A'
  },
  RowDataPacket {
    grade_id: 4,
    grade_name: '4',
    subject_id: 6,
    subject_name: 'Social',
    section_id: 209,
    section_name: 'A'
  }
]

Code:

let result = Object.values(results.reduce((a, { grade_id, grade_name, ...props }) => {
    if (!a[grade_id])
        a[grade_id] = Object.assign({}, { grade_id, grade_name: grade_name, sectionDetails: [{ section_id: props.section_id, section_name: props.section_name }] });
    else
        a[grade_id].sectionDetails.push({ section_id: props.section_id, section_name: props.section_name });
    return a;
}, {}));

console.log(result);

The output which I get:

{
    "status_code": 200,
    "status": "success",
    "data": [
        {
            "grade_id": 3,
            "grade_name": "3",
            "sectionDetails": [
                {
                    "section_id": 208,
                    "section_name": "A"
                },
                {
                    "section_id": 727,
                    "section_name": "B"
                }
            ],
        },
        {
            "grade_id": 4,
            "grade_name": "4",
            "sectionDetails": [
                {
                    "section_id": 209,
                    "section_name": "A"
                },
                {
                    "section_id": 924,
                    "section_name": "B"
                },
                {
                    "section_id": 209,
                    "section_name": "A"
                },
                {
                    "section_id": 209,
                    "section_name": "A"
                },
                {
                    "section_id": 209,
                    "section_name": "A"
                },
                {
                    "section_id": 209,
                    "section_name": "A"
                }
            ],
        },
        {
            "grade_id": 5,
            "grade_name": "5",
            "sectionDetails": [
                {
                    "section_id": 210,
                    "section_name": "A"
                }
            ],
        }
    ],
}

Expected schema:

[
    {
        "grade_id": "3",
        "grade_name": "3",
        "section_details": [
            {
                "section_id": "747",
                "section_name": "A",
                "subject_details": [
                    {
                        "subject_id": 3,
                        "subject_name": "Maths"
                    },
                    {
                        "subject_id": 4,
                        "subject_name": "Science"
                    }
                ],
            }
        ],
    },
    {
        "grade_id": "5",
        "grade_name": "5",
        "section_details": [
            {
                "section_id": "747",
                "section_name": "A",
                "subject_details": [
                    {
                        "subject_id": 3,
                        "subject_name": "Maths"
                    },
                    {
                        "subject_id": 4,
                        "subject_name": "Science"
                    }
                ],
            },
            {
                "section_id": "747",
                "section_name": "A",
                "subject_details": [
                    {
                        "subject_id": 3,
                        "subject_name": "Maths"
                    }
                ]
            }
        ],
    },
]


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source