编写一个名为sort_by_average_rating的函数,它将键值存储的列表/数组作为参数,其中每个键值存储都有键ratings、budget和box_office,其中budget和box_office是整数,ratings是整数列表。根据ratings中值的平均值对输入进行排序。
function sort_by_average_rating(lista){
for (var i of lista){
lista.sort((a,b)=>(a.ratings.reduce)/(b.ratings.length));
}
return lista;
}我怎么才能解决这个问题?
function sort_by_average_rating incorrect on input [[{'box_office': 35874798, 'ratings': [5, 2, 3, 9, 5, 2, 9], 'budget': 16277234.16}, {'box_office': 36159718, 'ratings': [9, 8, 2, 10, 6], 'budget': 14928243.11}, {'box_office': 45902132, 'ratings': [7, 7, 6], 'budget': 17305426.77}, {'box_office': 9920255, 'ratings': [8, 3, 9, 1, 9, 1], 'budget': 4214447.4}, {'box_office': 28163915, 'ratings': [1, 7, 8, 3, 8], 'budget': 5841329.67}, {'box_office': 5431090, 'ratings': [3, 1, 5, 1, 9, 1, 10], 'budget': 14396357.77}, {'box_office': 29674945, 'ratings': [1, 10, 2, 5], 'budget': 10909102.42}, {'box_office': 30136528, 'ratings': [1, 6, 1, 10], 'budget': 17685214.68}]]
result: [{'box_office': 35874798, 'ratings': [5, 2, 3, 9, 5, 2, 9], 'budget': 16277234.16}, {'box_office': 36159718, 'ratings': [9, 8, 2, 10, 6], 'budget': 14928243.11}, {'box_office': 45902132, 'ratings': [7, 7, 6], 'budget': 17305426.77}, {'box_office': 9920255, 'ratings': [8, 3, 9, 1, 9, 1], 'budget': 4214447.4}, {'box_office': 28163915, 'ratings': [1, 7, 8, 3, 8], 'budget': 5841329.67}, {'box_office': 5431090, 'ratings': [3, 1, 5, 1, 9, 1, 10], 'budget': 14396357.77}, {'box_office': 29674945, 'ratings': [1, 10, 2, 5], 'budget': 10909102.42}, {'box_office': 30136528, 'ratings': [1, 6, 1, 10], 'budget': 17685214.68}]
expected: [{'box_office': 5431090, 'ratings': [3, 1, 5, 1, 9, 1, 10], 'budget': 14396357.77}, {'box_office': 29674945, 'ratings': [1, 10, 2, 5], 'budget': 10909102.42}, {'box_office': 30136528, 'ratings': [1, 6, 1, 10], 'budget': 17685214.68}, {'box_office': 35874798, 'ratings': [5, 2, 3, 9, 5, 2, 9], 'budget': 16277234.16}, {'box_office': 9920255, 'ratings': [8, 3, 9, 1, 9, 1], 'budget': 4214447.4}, {'box_office': 28163915, 'ratings': [1, 7, 8, 3, 8], 'budget': 5841329.67}, {'box_office': 45902132, 'ratings': [7, 7, 6], 'budget': 17305426.77}, {'box_office': 36159718, 'ratings': [9, 8, 2, 10, 6], 'budget': 14928243.11}]发布于 2018-11-14 23:43:40
看看这样的事对你有用吗:
var data = [{ 'box_office': 35874798, 'ratings': [5, 2, 3, 9, 5, 2, 9], 'budget': 16277234.16 }, { 'box_office': 36159718, 'ratings': [9, 8, 2, 10, 6], 'budget': 14928243.11 }, { 'box_office': 45902132, 'ratings': [7, 7, 6], 'budget': 17305426.77 }, { 'box_office': 9920255, 'ratings': [8, 3, 9, 1, 9, 1], 'budget': 4214447.4 }, { 'box_office': 28163915, 'ratings': [1, 7, 8, 3, 8], 'budget': 5841329.67 }, { 'box_office': 5431090, 'ratings': [3, 1, 5, 1, 9, 1, 10], 'budget': 14396357.77 }, { 'box_office': 29674945, 'ratings': [1, 10, 2, 5], 'budget': 10909102.42 }, { 'box_office': 30136528, 'ratings': [1, 6, 1, 10], 'budget': 17685214.68 }]
const avg = (arr) => arr.reduce((r,c) => r + c) / arr.length
const result = data.sort((a,b) => avg(a.ratings) - avg(b.ratings))
console.log(result)
使用sort,然后使用助手函数(通过reduce添加所有条目并返回avg )。
https://stackoverflow.com/questions/53310383
复制相似问题