gpt4 book ai didi

javascript - 如何使用 JavaScript 按平均值正确排序输入?

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:39:27 25 4
gpt4 key购买 nike

编写一个名为 sort_by_average_rating 的函数,它将键值存储列表/数组作为参数,其中每个键值存储都有键 ratingsbudget box_office,其中 budgetbox_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}]

最佳答案

看看这样的东西是否适合你:

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 添加所有条目并返回平均值的辅助函数。

关于javascript - 如何使用 JavaScript 按平均值正确排序输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53310383/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com