I have user ratings for multiple courses for each user. I need to get the average user rating for each courseCode.
What would be the most efficient way to get these averages? The data can be found in either of the following two formats (though preferably the second one is better).
I was thinking of sorting the array and then aggregating values till I find a different courseCode, but I am not very good with array functions and I was wondering if there was a faster way to do this using hashmaps, .map
, sets
, reduce
. Please help me find an efficient solution as there are many users and I would want this to be as fast as possible so that the website loads quicker.
[
[
{courseCode: "SYD393", rating: 3},
{rating: 3, courseCode: "STA244"},
{courseCode: "STA255", rating: 5},
{rating: 5, courseCode: "CSE201"},
{courseCode: "CSE255", rating: 4},
{rating: 2, courseCode: "CSE202"},
{courseCode: "ASD323", rating: 5},
],
[
{courseCode: "ASD323", rating: 5},
{rating: 5, courseCode: "STA244"},
{courseCode: "STA255", rating: 5},
{courseCode: "SYD393", rating: 1},
],
//...more arrays for each user
];
[
[
{SYD393: 3},
{STA244: 4},
{STA255: 5},
{CSE255: 4},
{ASD323: 5},
],
[
{ASD323: 5},
{STA255: 5},
{SYD393: 1},
],
//...more arrays for each user
];