admin管理员组

文章数量:1291009

Below I reproduce my code for calculate average ratings for foods.

3+4+5+2 / 4 should be equal to 3.5. But this not gives me the correct output. What's wrong with my code?

const ratings = [
  {food:3},
  {food:4},
  {food:5},
  {food:2}
];

let food = 0;

ratings.forEach((obj,index)=>{
  food = (food + obj.food)/++index
})

console.log("FOOD",food)

Below I reproduce my code for calculate average ratings for foods.

3+4+5+2 / 4 should be equal to 3.5. But this not gives me the correct output. What's wrong with my code?

const ratings = [
  {food:3},
  {food:4},
  {food:5},
  {food:2}
];

let food = 0;

ratings.forEach((obj,index)=>{
  food = (food + obj.food)/++index
})

console.log("FOOD",food)

Share Improve this question edited Jul 9, 2019 at 4:37 Arnaud 7,44910 gold badges52 silver badges72 bronze badges asked Sep 26, 2018 at 8:20 margherita pizzamargherita pizza 7,18529 gold badges102 silver badges178 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 4

Despite of having several answers I would like to add mine focusing to improve the code quality:

  1. You can use destructuring in forEach() to just get the food property of the object as that is only the property you are interested with.
  2. Despite of dividing inside loop we can have only one division operation after the loop is pleted. This saves a lot of putation when the array is huge(thousands of objects)
  3. You can use short hand like += in the loop for summing up the value.
  4. You can make a single line code in forEach()

const ratings = [
  {food:3},
  {food:4},
  {food:5},
  {food:2}
];

let foodTotal = 0;
let length = ratings.length;
ratings.forEach(({food})=> foodTotal += food);

console.log("FOOD",foodTotal/length);

You'd use reduce to sum, then simply divide by rating's length

const ratings = [
  {food:3},
  {food:4},
  {food:5},
  {food:2}
];

const avg = ratings.reduce((a, {food}) => a + food, 0) / ratings.length;
console.log(avg);

Here is another solution using .reduce()

const ratings = [
  {food: 3},
  {food: 4},
  {food: 5},
  {food: 2}
]
const average = ratings.reduce((a, b) => {
  return {food: a.food + b.food}
}).food / ratings.length

console.log('FOOD', average)

You need to divide by the number of elements after you have summed them together

const ratings = [
    {food:3},
    {food:4},
    {food:5},
    {food:2}
];

let food = 0;

ratings.forEach((obj,index)=>{
    food = (food + obj.food)
});

food = food / ratings.length;

console.log("FOOD",food)

You need to add the relative contribution to the average of each food.

Since average is - sum of items / number of items in your case it's

(3+4+5+2) / 4

Which we can split to

3/4 + 4/4 + 5/4 + 2/4

const ratings = [{"food":3},{"food":4},{"food":5},{"food":2}];

let food = 0;

ratings.forEach((obj) => {
  food = food + obj.food / ratings.length;
})

console.log("FOOD", food)

You can also use Array.reduce() to shorten the code a bit:

const ratings = [{"food":3},{"food":4},{"food":5},{"food":2}];

const food = ratings.reduce((r, { food }) => 
  r + food
, 0) / ratings.length;

console.log("FOOD", food)

What you are doing is 3/1 + 4/2 + 5/3 + 2/4 which is not equivalent to 3/4 + 4/4 + 5/4 + 2/4. What you can do is

ratings.forEach((obj,index)=>{
    food = (food + obj.food)
});

food = food / ratings.length;

console.log("FOOD",food)

If you are into functional way of doing things then I would use a reduce function to get the result.

const food = ratings.reduce((sum, { food }) => sum + food, 0) / ratings.length;

console.log("FOOD", food)

本文标签: javascriptHow to get the average from array of objectsStack Overflow