更新时间:2023-11-07 14:34:52
尽管有几个答案,但我还是要重点关注我,以提高代码质量:
Despite of having several answers I would like to add mine focusing to improve the code quality:
forEach()
中使用分解来获取对象的food
属性,因为这只是您感兴趣的属性.+=
这样的短手来求和.forEach()
forEach()
to just get the food
property of the object as that is only the property you are interested with.+=
in the loop for summing up the value.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);