javascript - 尝试在我的数组中添加某些键的值以在我的 React 应用程序中使用
问题描述
我有一个对象数组。我想添加一个特定的密钥/对来添加累积存储的总数。
"AllocateStorage": 200,
+ "AllocateStorage": 585,
+ "AllocateStorage": 103547,
=104,242
[{
"MonitoringState": "disabled",
"State_Code": 16,
"State_Name": "running",
"EbsOptimized": false,
"EnaSupport": true,
"AllocateStorage": 200,
"SourceDestCheck": true,
"SpotInstanceRequestId": "None",
"SriovNetSupport": "None",
"StateReason_Code": "None",
"StateReason_Message": "None"
},
{
"MonitoringState": "disabled",
"State_Code": 16,
"State_Name": "stopped",
"EbsOptimized": false,
"EnaSupport": true,
"AllocateStorage": 585,
"SourceDestCheck": true,
"SpotInstanceRequestId": "None",
"SriovNetSupport": "None",
"StateReason_Code": "None",
"StateReason_Message": "None"
},
{
"MonitoringState": "disabled",
"State_Code": 16,
"State_Name": "running",
"EbsOptimized": false,
"EnaSupport": true,
"AllocateStorage": 103547,
"SourceDestCheck": true,
"SpotInstanceRequestId": "None",
"SriovNetSupport": "None",
"StateReason_Code": "None",
"StateReason_Message": "None"
}
]
.reduce
但示例仅显示添加整个数组。
解决方案
减少是要走的路:
let total = arr.reduce((total, obj) => {
return total += obj.AllocateStorage
}, 0);
推荐阅读
- bash - Bash 别名不保存超过一个会话?
- r - 每个人的子集重复项
- java - jframe中出现的“平方”字符
- mysql - 如何修复“java.sql.SQLException:找不到列 'id'。” Spring Boot 中的错误
- sql-server - 确定 SQL Server 表是否具有来自 MS Access VBA over ODBC 的 IDENTITY 列
- gradle - 如何从我的自定义 gradle 扩展访问嵌套配置值?
- python - 如何在被测的不同 python 源中模拟导入?
- html - 无法在小型设备中响应 div 中图像旁边的居中文本
- excel - 使用 excel 公式将列中的内容移过空列
- html - 在单独的 Div 中选择元素的多个通用同级元素