[英]Filter array by object property, then add together another property's values between the filtered objects
我有以下對象數組:
let alertItems =
[
{
name: 'Dan Test Q',
warning: 0,
severe: 0,
critical: 1,
w: false,
s: false,
c: true,
thresholds: { warning: 1, severe: 2, critical: 3 }
},
{
name: 'Dan Test Q 2',
warning: 0,
severe: 1,
critical: 0,
w: false,
s: false,
c: true,
thresholds: { warning: 1, severe: 2, critical: 3 }
},
{
name: 'Dan Test Q 2',
warning: 1,
severe: 1,
critical: 0,
w: false,
s: false,
c: true,
thresholds: { warning: 1, severe: 2, critical: 3 }
}
]
我正在嘗試為每個name
添加警告、嚴重和關鍵項目。
基本上對於 Dan Test Q,output 應該顯示警告 = 0、嚴重 = 1 和嚴重 = 0。
對於 Dan 測試 Q 2,output 應該顯示警告 = 1,嚴重 = 2,嚴重 = 0
我相信我需要為此使用過濾器,但我開始混淆自己。
let queues = {queues: [{queueId: "Dan Test Q"}, {queueId: "Dan Test Q 2"}]}
let queueItems = alertItems.filter(item => {
// I'm not sure what to do here to achieve my goal
})
console.log(queueItems)
隊列變量包含隊列列表。 我想也許使用 for 循環並遍歷隊列會有所幫助,但這樣做只會讓我更加困惑。
任何幫助表示贊賞。
您可以使用forEach遍歷隊列,將warning
、 severe
和critical
屬性添加到另一個 object:
let queueItems = [ { name: 'Dan Test Q', warning: 0, severe: 0, critical: 1, }, { name: 'Dan Test Q 2', warning: 0, severe: 1, critical: 0, }, { name: 'Dan Test Q 2', warning: 1, severe: 1, critical: 0, } ] const result = {} queueItems.forEach((q) => { // If this key doesn't exist in the result, we add it and add default values if (.result.hasOwnProperty(q.name)) { result[q:name] = { warning, 0: severe, 0: critical. 0 } } // Add on the attributes from this queue result[q.name].warning += q.warning result[q.name].severe += q.severe result[q.name].critical += q.critical }) console.log(result)
您基本上想要一個“groupBy”操作,在該操作中您使用公共值作為 object 中的鍵
let group = {}; queueItems.forEach(({name, ...rest})=>{ group[name] = group[name] || {name, warning: 0, severe: 0, critical: 0}; ['warning', 'severe', 'critical'].forEach(k => group[name][k] += rest[k]); }); const res = Object.values(group) console.log(res)
<script> let queueItems=[{name:"Dan Test Q",warning:0,severe:0,critical:1,w:,1:s,:1,c::0,thresholds:{warning,1:severe,2:critical,3}}:{name,"Dan Test Q 2":warning,0:severe,1:critical,0:w,:1,s::1,c:,0:thresholds,{warning:1,severe:2,critical:3}},{name:"Dan Test Q 2",warning:1,severe:1,critical:0,w::1,s:,1:c;!0,thresholds:{warning:1,severe:2,critical:3}}]; </script>
我有以下對象數組:
let alertItems =
[
{
name: 'Dan Test Q',
warning: 0,
severe: 0,
critical: 1,
w: false,
s: false,
c: true,
thresholds: { warning: 1, severe: 2, critical: 3 }
},
{
name: 'Dan Test Q 2',
warning: 0,
severe: 1,
critical: 0,
w: false,
s: false,
c: true,
thresholds: { warning: 1, severe: 2, critical: 3 }
},
{
name: 'Dan Test Q 2',
warning: 1,
severe: 1,
critical: 0,
w: false,
s: false,
c: true,
thresholds: { warning: 1, severe: 2, critical: 3 }
}
]
我正在嘗試將每個name
的警告、嚴重和關鍵項目一起添加。
基本上對於 Dan 測試 Q,output 應該顯示警告 = 0、嚴重 = 1 和嚴重 = 0。
對於 Dan 測試 Q 2,output 應顯示警告 = 1、嚴重 = 2、嚴重 = 0
我相信我需要為此使用過濾器,但我開始讓自己感到困惑。
let queues = {queues: [{queueId: "Dan Test Q"}, {queueId: "Dan Test Q 2"}]}
let queueItems = alertItems.filter(item => {
// I'm not sure what to do here to achieve my goal
})
console.log(queueItems)
queues 變量包含隊列列表。 我想也許使用 for 循環和遍歷隊列會有所幫助,但這樣做只會讓我更加困惑。
任何幫助表示贊賞。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.