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
];
Looks like this is similar to the answer you posted yourself, except I separated out the averaging of the Map into its own step.
const data = [[{ SYD393: 3 }, { STA244: 4 }, { STA255: 5 }, { CSE255: 4 }, { ASD323: 5 },], [{ ASD323: 5 }, { STA255: 5 }, { SYD393: 1 },],];
const avg = a => a.reduce((a, b) => a + b, 0) / a.length,
sums = data.flat().reduce((a, o) => {
const [[c, r]] = Object.entries(o);
a.set(c, (a.get(c) ?? []).concat(r));
return a;
}, new Map),
avgs = new Map([...sums.entries()].map(([c, rs]) => [c, avg(rs)])),
res = data.map(us => us.map(o => {
const [k] = Object.keys(o); return { [k]: avgs.get(k) }
}));
console.log(res);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Though it is more straightforward using your first data shape as there is no need to wrangle the Object.entries/keys
.
const data = [[{ 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 },],];
const avg = a => a.reduce((a, b) => a + b, 0) / a.length,
sums = data.flat().reduce((a, { courseCode: c, rating: r }) =>
(a.set(c, (a.get(c) ?? []).concat(r)), a), new Map),
avgs = new Map([...sums.entries()].map(([c, rs]) => [c, avg(rs)])),
res = data.map(us => us.map(({ courseCode: c }) => ({ courseCode: c, rating: avgs.get(c) })));
console.log(res);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments