Ad

Imagine you were given the task to collate a log of student attendance. The log contains different courses that each student attended. Group every student that attended the same course together
[
{subject:'English', student:'Odunola'},
{subject:'Math', student:'Wale'},
{subject:'Chemistry', student:'Wasiu'},
{subject:'Math', student:'Bola'},
{subject:'Biology', student:'Segun'},
{subject:'Chemistry', student:'Ramoni'},
{subject:'Physics', student:'Bolu'},
{subject:'Math', student:'Bose'},
]
Output should look like this
[
{subject: 'English', student: ['Odunola']},
{subject: 'Math', student: ['Wale', 'Bola', 'Bose']},
{subject: 'Chemistry', student: ['Wasiu', 'Ramoni']},
{subject: 'Biology', student: ['Segun']},
{subject: 'Biology', student: ['Segun']},
{subject: 'Physics', student: ['Bolu']},
]

function collate(arr){
  let store = {};
    arr.forEach(one =>{
       if(!store[one['course']]){
         store[one['course']] = {course: one['course'], name: [].concat(one['name'])};
       }else{
          store[one['course']]['name'].push(one['name'])
       }
    })
    return Object.values(store);
}