我想迭代一系列不同的評級,按每個唯一的 id 排序,求和并計算每個 id 評級的平均值。然后將平均值保存在一個新陣列中,我可以在其中呼叫類似 averageRating[i] 的東西,其中每個條目將是每個 id 的評分。
原始物件陣列如下所示,其中 id 可以是任意數字。
data = [{id: 1, rating: 1}, {id: 1, rating: 3}, {id: 1, rating: 1}, {id: 1, rating: 4}, {id: 1, rating}, {id: 2, rating: 3}, {id: 3, rating: 5}, {id: 1, rating: 5}, {id: 1, rating: 5}, {id: 1, rating: 5, {id: 1, rating: 1}, {id: 2, rating: 4}, {id: 1, rating: 3}, {id: 1, rating: 2}]
我只用一個特定的 id 就可以完成這項作業,執行如下操作,但是在弄清楚如何處理動態數量的 id 時遇到了一些麻煩。
var [average, updateAverage] = useState(0);
let ratings = data.map((item) => item.rating);
// Calculate average of the array
let sum = ratings.reduce((a, b) => a b, 0);
let avg = sum / ratings.length || 0;
let avgRounded = Math.round(avg); // Round to nearest whole number
updateAverage = avgRounded;
uj5u.com熱心網友回復:
這可能超過了最高性能,但如果你喜歡一點數學,你實際上可以在一個回圈中完成它。看到這個數學解決方案
const avgCount = {} // This is a temp var needed for accumulative averaging mathematical formula
// THIS VAR HAS THE ANSWER IN
const averages = data.reduce((averages, individualRating) => {
// We need a counter of the number of times this ID has been seen already. This is needed by the algorithm.
// Now we have seen a new rating with this id, increase the counter to reflect that.
// If it's not there we start from 1.
avgCount[individualRating.id] = (avgCount[individualRating.id] ?? 0) 1
// Get the current rolling average. If there isn't one (this is first rating with this id we have seen), then its just the rating of this current item.
const currAccumulatedAverage = averages[individualRating.id] ?? individualRating.id
// Calculate the new rolling average from the previous one
averages[individualRating.id] = ((currAccumulatedAverage * (avgCount[individualRating.id] - 1)) individualRating.rating) / avgCount[individualRating.id]
return averages
}, {})
這應該是高性能的,因為沒有多個回圈或中間結構。
對于此輸入:
let data = [{id: 1, rating: 1}, {id: 1, rating: 3}, {id: 1, rating: 1}, {id: 1, rating: 4}, {id: 1, rating: 1}, {id: 2, rating: 3}, {id: 3, rating: 5}, {id: 1, rating: 5}, {id: 1, rating: 5}, {id: 1, rating: 5}, {id: 1, rating: 1}, {id: 2, rating: 4}, {id: 1, rating: 3}, {id: 1, rating: 2}]
它回傳
{1: 2.8181818181818183, 2: 3.5, 3: 5}
uj5u.com熱心網友回復:
您可以使用常規回圈并進行所有處理。閱讀行內評論:
// Rating data
const data = [
{id: 1, rating: 1},
{id: 1, rating: 3},
{id: 1, rating: 1},
{id: 1, rating: 4},
{id: 1, rating: 2},
{id: 2, rating: 3},
{id: 3, rating: 5},
{id: 1, rating: 5},
{id: 1, rating: 5},
{id: 1, rating: 5},
{id: 1, rating: 1},
{id: 2, rating: 4},
{id: 1, rating: 3},
{id: 1, rating: 2}
];
// Create new object for id -> average ratings
const avr = {};
// Iterate data array
for(const item of data) {
// Create object of arrays
// for calculate avarage later
if(avr[item.id]) avr[item.id].push(item.rating);
else avr[item.id] = [item.rating];
}
// Iterate object and calculate average values
for(const el in avr) {
// Reduce
const sum = avr[el].reduce((a, b) => a b, 0);
// Calculate average and update object
avr[el] = Math.round(sum / avr[el].length || 0);
}
// Test
console.log(avr);
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/513150.html
標籤:javascript反应
