您好我正在尝试按值对对象数组进行排序,使用一个函数来计算该对象属性中值的平均值,该属性是一个整数数组,然后按递减顺序对其进行排序。
// SORT BY SCORE BY MOST SCORED
function sortByScoreDown() {
arrayLivros.sort((a, b) => fullscoreForSort(b._scores) - fullscoreForSort(a._scores));
}
// CALCULATE FULLSCORE
function fullscoreForSort(givenScores) {
let score = 0;
let total = givenScores.length - 1; // -1 BECAUSE BOOK._SCORES STARTS WITH AN ARRAY WITH 0 AS FIRST VALUE FOR SIMPLIFICATION
if (total != 0) {
let summedScore = givenScores.reduce((sum, add) => sum + add);
let score = summedScore / total;
}
return score;
}
// VALUES
newBook1 = {_title: book1,
_scores: [0,100,50]}
newBook2 = {_title: book2,
_scores: [0,100,100]}
newBook3 = {_title: book3,
_scores: [0,50,50]}
newBook4 = {_title: book3,
_scores: [0,30,30]}
arrayBooks = [newBook1, newBook2, newBook3, newBook4];
// EXCPECTED RETURN
arrayBooks = [newBook2, newBook1, newBook3, newBook4];
提前致谢。