Issue
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"
}
]
}
],
},
]
Solution
Concept:
- Group by
grade_id
andsection_id
forsubjects
. - Group by
grade_id
from result (1).
let groupedByGradeSection = Object.values(results.reduce((a:{ [key: string]: any }, { grade_id, grade_name, ...props }) => {
let key = `Grade_${grade_id}_Section_${props.section_id}`;
if (!a[key])
a[key] = Object.assign({}, { grade_id, grade_name: grade_name, section_id: props.section_id, section_name: props.section_name, subject_details: [{ subject_id: props.subject_id, subject_name: props.subject_name }] });
else
a[key].subject_details.push({ subject_id: props.subject_id, subject_name: props.subject_name });
return a;
}, {}));
let result = Object.values(groupedByGradeSection.reduce((a:{ [key: string]: any }, { grade_id, grade_name, ...props }) => {
if (!a[grade_id])
a[grade_id] = Object.assign({}, { grade_id, grade_name: grade_name, section_details: [{ section_id: props.section_id, section_name: props.section_name, subject_details: props.subject_details }] });
else
a[grade_id].section_details.push({ section_id: props.section_id, section_name: props.section_name, subject_details: props.subject_details });
return a;
}, {}));
console.log(result);
Sample Demo on Typescript Playground
Answered By - Yong Shun
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.