reactjs - 使用带有材料 UI 的反应钩子的表中列的平均总值
问题描述
我正在尝试在表格底部显示列的平均值。
像这样的行
const rows = [
createData("Frozen yoghurt", 159, 6.0, 24, 4.0),
createData("Ice cream sandwich", 237, 9.0, 37, 4.3),
createData("Eclair", 262, 16.0, 24, 6.0),
createData("Cupcake", 305, 3.7, 67, 4.3),
createData("Gingerbread", 356, 16.0, 49, 3.9)
];
并通过行映射
<TableCell>
{rows.map((row) => (
<TableCell align="right">
{row.calories}
</TableCell>
))}
</TableCell>
<TableCell>
total array: {rows.length} <br />
</TableCell>
沙盒上的完整代码
感谢任何帮助
解决方案
我想这应该对你有用......
var average_cal = 0; // Average Calories
var total_cal = 0; // Total Calories
forEach(var row in rows){
total_cal = total_cal + row.calories ; // Finding the Total Calories
}
avrage_cal = total_cal / rows.length; // Calculating the average claories
<TableCell>{average_cal}</TableCell> // Display the result in the cell
注意:您可以在适当的位置添加代码。这只是为了说明如何计算一列的平均值。您可以对所有列执行相同的操作。
推荐阅读
- java - 使用 Processing 检测 Android 上的捏合动作
- authentication - 突然的身份验证错误 ActionDispatch::Http::Parameters::ParseError (743: '' 处的意外令牌):
- python - 在列表中搜索数组中的某些字符串
- python - Python 代码有时会被执行,有时不会
- javascript - 用于捕获嵌套括号中的值的正则表达式
- angular - mat-cell如何绑定源给mat-table
- c# - 使用 .Net Core 在 Linux 中读取另一个进程的内存
- google-cloud-platform - GCP:云 NAT:为什么附加到云 NAT 的区域 IP 地址被标记为“未使用”?
- javascript - 如何从 redux 存储中的数组中删除特定元素
- unit-testing - 如何修复这些错误而不是在 qarsar 测试中用 jest 导入某些模块?