我有一个对象数组,我需要查看两个属性才能得到一个值。
let limit = [
{
NodeId: "9837f279",
NodeName: "Node1",
summary: {
current: 50,
limit: 75
}
}, {
NodeId: "4189f279",
NodeName: "Node2",
summary: {
current: 60,
limit: 100
}
}, {
NodeId: "9837f279",
NodeName: "Node1",
summary: {
current: 30,
limit: 75
}
}
]
这里我需要添加所有节点:
summary.current / summary.limit = (50 + 60 + 30) / (75 + 100 + 75)
如何使用JavaScript执行此操作?
答案 0 :(得分:1)
我建议用Array#reduce使自己熟悉。
const totalCurrent = limit.reduce((sum, item) => sum + item.summary.current, 0);
const totalLimit = limit.reduce((sum, item) => sum + item.summary.limit, 0);
const totalUtilisation = totalCurrent / totalLimit;