I have an array with different objects where each object is an classes. Looks like this:
sessions = [
{ session_id: 1, time: '09:00', student: { student_id: 1, name: 'Adi' }, class: { class_id: 1, name: 'A' } },
{ session_id: 2, time: '10:00', student: { student_id: 7, name: 'Dede' }, class: { class_id: 4, name: 'D' } },
{ session_id: 1, time: '09:00', student: { student_id: 3, name: 'Bayu' }, class: { class_id: 2, name: 'B' } },
{ session_id: 1, time: '09:00', student: { student_id: 2, name: 'Budi' }, class: { class_id: 1, name: 'A' } },
{ session_id: 2, time: '10:00', student: { student_id: 3, name: 'Maha' }, class: { class_id: 3, name: 'C' } },
];
I want to change the data to be like this, (grouping by session_id or time):
sessions = [
{
session_id: 1,
time: '09:00',
classes: [
{
class_id: 1,
name: 'A',
students: [
{ student_id: 1, name: 'Adi' },
{ student_id: 1, name: 'Budi' },
],
},
{
class_id: 2,
name: 'B',
students: [
{ student_id: 3, name: 'Bayu' },
],
},
],
},
{
session_id: 2,
time: '10:00',
classes: [
{
class_id: 3,
name: 'C',
students: [
{ student_id: 6, name: 'Maha' },
],
},
{
class_id: 4,
name: 'D',
students: [
{ student_id: 7, name: 'Dede' },
],
},
],
},
];
I'm tired of trying to solve it, because my javascript isn't that great. please help me to solve the code.
CodePudding user response:
You can use lodash.groupBy. For example:
const sessions = [
{ session_id: 1, time: '09:00', student: { student_id: 1, name: 'Adi' }, class: { class_id: 1, name: 'A' } },
{ session_id: 2, time: '10:00', student: { student_id: 7, name: 'Dede' }, class: { class_id: 4, name: 'D' } },
{ session_id: 1, time: '09:00', student: { student_id: 3, name: 'Bayu' }, class: { class_id: 2, name: 'B' } },
{ session_id: 1, time: '09:00', student: { student_id: 2, name: 'Budi' }, class: { class_id: 1, name: 'A' } },
{ session_id: 2, time: '10:00', student: { student_id: 3, name: 'Maha' }, class: { class_id: 3, name: 'C' } },
];
const result = Object.entries(_.groupBy(sessions, 'session_id')).map(session => ({ session_id: Number(session[0]), time: session[1][0].time, classes: Object.entries(_.groupBy(session[1], s => s.class.class_id)).map(c => ({ class_id: Number(c[0]), name: c[1][0].class.name, students: c[1].map(x => ({ student_id: x.student.student_id, name: x.student.name })) })) }))
console.log(result)
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
CodePudding user response:
Try below snippet:
const sessions = [
{ session_id: 1, time: '09:00', student: { student_id: 1, name: 'Adi' }, class: { class_id: 1, name: 'A' } },
{ session_id: 2, time: '10:00', student: { student_id: 7, name: 'Dede' }, class: { class_id: 4, name: 'D' } },
{ session_id: 1, time: '09:00', student: { student_id: 3, name: 'Bayu' }, class: { class_id: 2, name: 'B' } },
{ session_id: 1, time: '09:00', student: { student_id: 2, name: 'Budi' }, class: { class_id: 1, name: 'A' } },
{ session_id: 2, time: '10:00', student: { student_id: 3, name: 'Maha' }, class: { class_id: 3, name: 'C' } },
];
const newSessions = sessions.reduce((acc, session)=> {
const sessionIndex = acc.findIndex(item => item.session_id == session.session_id);
if(sessionIndex > -1){
updateSessionClasses(acc[sessionIndex], session)
}else {
acc.push({ session_id: session.session_id, time: session.time, classes: [{...session.class, students: [session.student]}] })
};
return acc;
}, []);
function updateSessionClasses(storedSession, session){
const classIndex = storedSession.classes.findIndex(c => c.class_id == session.class.class_id);
if(classIndex > -1){
storedSession.classes[classIndex].students.push(session.student)
}else {
storedSession.classes.push({...session.class, students: [session.student]})
}
}
console.log(newSessions)
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>