The data returned from the API call is structured as follows:
var array = {"scores":[
{ userid: "1", mark: 4.3 },
{ userid: "1", mark: 3.8 },
{ userid: "2", mark: 4.6 },
{ userid: "2", mark: 4.1 },
{ userid: "2", mark: 3.9 },
{ userid: "2", mark: null}
]};
The desired output requires matching each userid with the average of their marks and total count, excluding any null values for mark.
var arr = [
{ userid: "1", mark: 4.05, count: 2 },
{ userid: "2", mark: 4.2, count: 3}
]
I attempted to achieve this with a script, but encountered an error - 'array.scores[0].reduce is not a function'
result = Array.from(
array.scores[0].reduce((m, { userid, mark}) => {
var temp = m.get(userid) || { sum: 0, count: 0 };
temp.sum += mark;
temp.count++;
return m.set(userid, temp);
}, new Map),
([externalCourse, { sum, count }]) => ({ userid, average: sum / count, count })
);
console.log(result);