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
6 Answers
Reset to default 4Despite of having several answers I would like to add mine focusing to improve the code quality:
- You can use destructuring in
forEach()
to just get thefood
property of the object as that is only the property you are interested with. - 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)
- You can use short hand like
+=
in the loop for summing up the value. - 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
版权声明:本文标题:javascript - How to get the average from array of objects - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741514120a2382780.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论