Issue
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 }
]
Desired / target schema
let gradeDetails = [{
grade_id: 3,
section_details: [{
section_id: 747,
subject_details: [{subject_id: 3}]
}]
}, {
.
.
.
}, {
grade_id: 7
section_details: [{
section_id: 751,
subject_details: [
{subject_id: 3},
{subject_id: 4}
]
}]
}, {
grade_id: 8,
section_details: [{
section_id: 752,
subject_details: [{subject_id: 3}]
]}
}];
Solution
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 usingnew Set()and iterate over each grade - For each grade, construct an object to be
pushed to the result grade_idis set togradesection_detailsis set as an array filtered by matchinggrade_idsubect_detailsis an array with exactly one element (ie, thesubject_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());
const newGetGradeDetails = (arr = user) => (
Object.entries(
arr.reduce((fin, itm) => ({
...fin,
[itm.grade_id]: {
...fin[itm.grade_id],
[itm.section_id]: {
...(fin && fin[itm.grade_id] && fin[itm.grade_id][itm.section_id]
? fin[itm.grade_id][itm.section_id]
: {}
),
subjects: [
...(fin && fin[itm.grade_id] &&
fin[itm.grade_id][itm.section_id] &&
fin[itm.grade_id][itm.section_id].subjects &&
Array.isArray(fin[itm.grade_id][itm.section_id].subjects)
? fin[itm.grade_id][itm.section_id].subjects
: []
),
itm.subject_id
]
}
}
}) ,{})
).map(([kg, vg]) => ({
grade_id: kg,
section_details: Object.entries(vg).map(([kse, vse]) => ({
section_id: kse,
subject_details: vse.subjects.map(su => ({
subject_id: su
}))
}))
}))
);
console.log('\n\tUpdated as per comments noted below\n\n', newGetGradeDetails());
Edit: Added new method to account for changes requested on comments below.
Answered By - jsN00b
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.