[英]Filter array by object property, then add together another property's values between the filtered objects
I have the following array of 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 }
}
]
I am trying to add the warning, severe, and critical items together for each name
.我正在尝试为每个
name
添加警告、严重和关键项目。
Basically for Dan Test Q the output should show that warning = 0, severe = 1 and critical = 0.基本上对于 Dan Test Q,output 应该显示警告 = 0、严重 = 1 和严重 = 0。
For Dan Test Q 2 the output should show that warning = 1, severe = 2, critical = 0对于 Dan 测试 Q 2,output 应该显示警告 = 1,严重 = 2,严重 = 0
I believe I need to use filter for this, but I'm starting to confuse myself.我相信我需要为此使用过滤器,但我开始混淆自己。
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)
The queues variable contains the list of queues.队列变量包含队列列表。 I thought perhaps using a for loop and looping over the queues would help, but doing that just confused me even more.
我想也许使用 for 循环并遍历队列会有所帮助,但这样做只会让我更加困惑。
Any help is appreciated.任何帮助表示赞赏。
You can use forEach to iterate over the queues, adding the warning
, severe
and critical
properties to another object:您可以使用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)
You are basically wanting a "groupBy" operation where you use the common vales as keys in an object您基本上想要一个“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>
I have the following array of 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 }
}
]
I am trying to add the warning, severe, and critical items together for each name
.我正在尝试将每个
name
的警告、严重和关键项目一起添加。
Basically for Dan Test Q the output should show that warning = 0, severe = 1 and critical = 0.基本上对于 Dan 测试 Q,output 应该显示警告 = 0、严重 = 1 和严重 = 0。
For Dan Test Q 2 the output should show that warning = 1, severe = 2, critical = 0对于 Dan 测试 Q 2,output 应显示警告 = 1、严重 = 2、严重 = 0
I believe I need to use filter for this, but I'm starting to confuse myself.我相信我需要为此使用过滤器,但我开始让自己感到困惑。
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)
The queues variable contains the list of queues. queues 变量包含队列列表。 I thought perhaps using a for loop and looping over the queues would help, but doing that just confused me even more.
我想也许使用 for 循环和遍历队列会有所帮助,但这样做只会让我更加困惑。
Any help is appreciated.任何帮助表示赞赏。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.