Skip to content

Instantly share code, notes, and snippets.

@talha08
Last active January 31, 2019 19:37
Show Gist options
  • Save talha08/040c65946b9a87bcc11d4867313c7ec8 to your computer and use it in GitHub Desktop.
Save talha08/040c65946b9a87bcc11d4867313c7ec8 to your computer and use it in GitHub Desktop.
JS Bin// source https://jsbin.com/xifegud
let courses = [
{
"title":"ttile 1",
"id":"1",
"course_cat":"introduction"
},
{
"title":"title 2",
"id":"2",
"course_cat":null
},
{
"title":"blabla 1",
"id":"3",
"course_cat":"test"
},
{
"title":"blabla 2",
"id":"4",
"course_cat":null
}
];
let grouped = courses.reduce((allCourses, course) => {
allCourses[course.course_cat] = [...allCourses[course.course_cat] || [], course];
return allCourses;
}, {});
console.log(grouped);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment