Q:

Javascript filter data based on the ID

I have an object that contains these data.
 {id: 1864,
 courseContentId: 481,
 fileName: GymMembership.jpg
 }
 {id: 1865,
 courseContentId: 481,
 fileName: Field.jpg
 }
 {id: 1866,
 courseContentId: 482,
 fileName: Track.jpg
 }

And i wanted to show them seperately on the view based on the courseContentId by creating a new object and this is my desired output.
 {id: 1864,
 courseContentId: 481,
 fileName: GymMembership.jpg
 }
 {id: 1865,
 courseContentId: 481,
 fileName: Field.jpg
 }

 {id: 1866,
 courseContentId: 482,
 fileName: Track.jpg
 }

What kind of javascript function is suitable for this? 

A:

Just use _.GroupBy, rather than applying multiple filters.

courses = [{id: 1864,
 courseContentId: 481,
 fileName: 'GymMembership.jpg'
 },
 {id: 1865,
 courseContentId: 481,
 fileName: 'Field.jpg'
 },
 {id: 1866,
 courseContentId: 482,
 fileName: 'Track.jpg'
 }]
 
var grouped = _.groupBy(courses, function(course) {
  return course.courseContentId;
});
console.log("Grouped")
console.log(grouped);
console.log("Filter by 481")
console.log(grouped["481"]);
console.log("Filter by 482")
console.log(grouped["482"]);
<script src="https://cdn.jsdelivr.net/lodash/4.17.2/lodash.min.js"></script>

