编写一个名为sort_by_average_rating
的函数,它将键值存储的列表/数组作为参数,其中每个键值存储具有键ratings
,budget
和box_office
,其中budget
和box_office
是整数,rating是整数列表。根据ratings
中值的平均值对输入进行排序。
当密钥等于["ratings"]
时,我尝试在for循环中添加所有内容然后在那之后,我平均两个for循环的结果。最后,我使用了一个单独的函数来排序所有内容。
function key(a,b){
for(var i = 0; i < a.length; i++){
tol1 += a[i]["ratings"];
for(var x = 0; x < b.length; x++) {
tol2 += b[x]["ratings"];
}
var average = (tol1/tol2);
}
return average;
}
function key(x,y){
if (x[key] < x[key]) { return -1; }
if (y[key] > y[key]) { return 1; }
return 0;
}
function sort_by_average_rating(b){
b.sort(key)
}
结果:
[
{'box_office': 12574015, 'budget': 3986053.18, 'ratings': [8, 7, 1]},
{'box_office': 44855251, 'budget': 3301717.62, 'ratings': [7, 1, 1]},
{'box_office': 36625133, 'budget': 8678591, 'ratings': [7, 6, 2, 8]},
{'box_office': 48397691, 'budget': 15916122.88, 'ratings': [7, 3, 8, 8, 6, 8]},
{'box_office': 43344800, 'budget': 4373679.25, 'ratings': [1, 1, 7, 4]}
]
预期:
[
{'box_office': 44855251, 'budget': 3301717.62, 'ratings': [7, 1, 1]},
{'box_office': 43344800, 'budget': 4373679.25, 'ratings': [1, 1, 7, 4]},
{'box_office': 12574015, 'budget': 3986053.18, 'ratings': [8, 7, 1]},
{'box_office': 36625133, 'budget': 8678591.0, 'ratings': [7, 6, 2, 8]},
{'box_office': 48397691, 'budget': 15916122.88, 'ratings': [7, 3, 8, 8, 6, 8]}
]
一种可能的解决方案是创建一个返回average
数组的ratings
的函数,我们将使用Array.reduce()这个。然后你可以使用内置的Array.sort()这个新函数来生成你想要的结果:
const input = [
{'box_office': 12574015, 'budget': 3986053.18, 'ratings': [8, 7, 1]},
{'box_office': 44855251, 'budget': 3301717.62, 'ratings': [7, 1, 1]},
{'box_office': 36625133, 'budget': 8678591, 'ratings': [7, 6, 2, 8]},
{'box_office': 48397691, 'budget': 15916122.88, 'ratings': [7, 3, 8, 8, 6, 8]},
{'box_office': 43344800, 'budget': 4373679.25, 'ratings': [1, 1, 7, 4]}
];
const getAverage = (arr) =>
{
return (Array.isArray(arr) && arr.length > 0) ?
arr.reduce((acc, n) => acc + n) / arr.length :
0;
};
input.sort((a, b) => getAverage(a.ratings) - getAverage(b.ratings));
console.log(input);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}
为避免原始数据的突变,请使用:
let sorted = input.slice().sort((a, b) => getAverage(a.ratings) - getAverage(b.ratings));
我建议使用不同的方法(sorting with map),通过获取数组中的平均值和索引的另一个数组,对indices数组进行排序并通过获取索引来映射对象。
const add = (a, b) => a + b;
var array = [{ box_office: 12574015, budget: 3986053.18, ratings: [8, 7, 1] }, { box_office: 44855251, budget: 3301717.62, ratings: [7, 1, 1] }, { box_office: 36625133, budget: 8678591, ratings: [7, 6, 2, 8] }, { box_office: 48397691, budget: 15916122.88, ratings: [7, 3, 8, 8, 6, 8] }, { box_office: 43344800, budget: 4373679.25, ratings: [1, 1, 7, 4] }],
averages = array.map(({ ratings }) => ratings.reduce(add, 0) / ratings.length),
indices = [...averages.keys()].sort((i, j) => averages[i] - averages[j]),
result = indices.map(i => array[i]);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
1)你应该在使用它们之前初始化你的变量(例如tol1
2)你应该避免重复功能名称(例如key
)
3)你应该将你的变量和函数命名为有意义的东西(例如'a','b','key','x','y')什么都不是。
let movies = [
{'box_office': 12574015, 'budget': 3986053.18, 'ratings': [8, 7, 1]},
{'box_office': 44855251, 'budget': 3301717.62, 'ratings': [7, 1, 1]},
{'box_office': 36625133, 'budget': 8678591, 'ratings': [7, 6, 2, 8]},
{'box_office': 48397691, 'budget': 15916122.88, 'ratings': [7, 3, 8, 8, 6, 8]},
{'box_office': 43344800, 'budget': 4373679.25, 'ratings': [1, 1, 7, 4]}
];
let averageRating = movie =>
movie.ratings.reduce((rating, sum) => rating + sum, 0) / movie.ratings.length;
let sortedMovies = movies.sort((movie1, movie2) => averageRating(movie1) - averageRating(movie2));
console.log(sortedMovies);