繁体   English   中英

按 object 属性过滤数组,然后将过滤对象之间的另一个属性的值加在一起

[英]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遍历队列,将warningseverecritical属性添加到另一个 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM