Код: Выделить всё
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"]];
До сих пор я использовал функцию уменьшения() для массива, чтобы подсчитайте сумму всех оценок, но не могу понять, как получить количество оценок, чтобы разделить на него.
Код: Выделить всё
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"]](Если это имеет значение, сценарий написан на Apps Script, я получаю значения для seriesList из листа моего файла Sheets, и я печатаю результат на другой лист. Вот почему мне нужен 2D-массив.)
Подробнее здесь: https://stackoverflow.com/questions/793 ... ing-number