计算平均评论 javascript

Working out average reviews javascript

我正在尝试遍历 3 个地方和 select 平均评论和评分最高的地方。

假设我有以下内容。

var places = [{
  name: "place 1",
  reviews: 100,
  rating: 5,
},{
  name: "place 2",
  reviews: 10000,
  rating: 5,
},{
  name: "place 3",
  reviews: 10000000,
  rating: 4,
}];

for (i = 0; i < places.length; i++) { 

    // loop through and calculate the highest average reviews
    var rating = places[i].rating;
    var reviews = places[i].reviews;

    // work out the average score place 3 should be the highest

}

http://jsbin.com/loyequluke/edit?js,console,output

我想要做的任何建议是在 3 个地方中找到最高的平均评分。

正确的结果应该是第 3 位,但我不知道如何解决这个问题,请帮忙吗?

请检查下面的代码,让我知道这是否适合您。由于我不知道您是如何计算最高分的,因此我假设它是 (rating * reviews) / rating,您可以根据它获得该值。您可以 运行 给定的代码片段并亲自查看结果。基本上,你有计算的想法,这对几百的小记录最有效。

var places = [{
  name: "place 1",
  reviews: 100,
  rating: 5,
},
{
  name: "place 3",
  reviews: 30000000,
  rating: 23,
},

{
  name: "place 2",
  reviews: 10000,
  rating: 5,
},{
  name: "place 3",
  reviews: 10000000,
  rating: 4,
}];

var highest = [];
for (i = 0; i < places.length; i++) { 
    
    // loop through and calculate the highest average reviews
    var rating = places[i].rating;
    var reviews = places[i].reviews;

    highest.push((rating * reviews) / rating);
  
    // work out the average score place 3 should be the highest

}

var highestRating = highest[0];
var pos = 0;

for (i = 0; i < highest.length; i += 1) {
    if (highestRating < highest[i]) {
        highestRating = highest[i];
        pos = i;
    }
}

console.log('Highest Rating: ', highestRating);
console.log('Found at position: ', pos);
console.log('Place with highest score : ', places[pos]);

让我们知道这是否适合您。