我有一个对象数组。我想添加一个特定的 key /对的累积存储总量。
“分配存储”:200,
+ “分配存储”:585,
+ “分配存储”: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);
关于javascript - 尝试添加数组中某些键的值以在我的 React 应用程序中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50414990/