I have a 2d array which contains a list of series with title, rating, and time spent watching. For example:
let seriesList = [["Squid Game", 3.5, "4 days"], ["Squid Game", 2.5, "3 days"], ["Buffy the Vampire Slayer", 5, "32 days"], ["The Fosters", 3, "5 days"]];
A series can appear multiple times with a different rating. I would like to calculate the average rating for each series (and get another 2d array with each series appearing only once).
So far, I used reduce() on the array in order to do the sum of all the ratings, but I can’t figure out how to get the number of ratings so I can divide by it.
let seriesList = [["Squid Game", 3.5, "4 days"], ["Squid Game", 2.5, "3 days"], ["Buffy the Vampire Slayer", 5, "32 days"], ["The Fosters", 3, "5 days"]]; // example of values in my array, but it's actually much longer and it will change with each new series i watch
let seriesListAverage = Object.values(
seriesList.reduce((accumulator, currentValue) => {
if (accumulator[currentValue[0]]) {
accumulator[currentValue[0]][1] += currentValue[1]; // do the sum of all ratings
}
else {
accumulator[currentValue[0]] = currentValue;
}
return accumulator;
}, {})
);
console.log(seriesListAverage); // Result: [["Squid Game", 6, "4 days"], ["Buffy the Vampire Slayer", 5, "32 days"], ["The Fosters", 3, "5 days"]]
// Expected result: [["Squid Game", 3, "4 days"], ["Buffy the Vampire Slayer", 5, "32 days"], ["The Fosters", 3, "5 days"]]
(If it matters, the script is written in Apps Script, I get the values for seriesList from a sheet of my Sheets file, and I print the result into another sheet. That’s why I need a 2d array.)
2
Answers
You could group first and then get the average.