简体   繁体   中英

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 .

Basically for Dan Test Q the output should show that warning = 0, severe = 1 and critical = 0.

For Dan Test Q 2 the output should show that warning = 1, severe = 2, critical = 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.

Any help is appreciated.

You can use forEach to iterate over the queues, adding the warning , severe and critical properties to another 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

 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 .

Basically for Dan Test Q the output should show that warning = 0, severe = 1 and critical = 0.

For Dan Test Q 2 the output should show that warning = 1, severe = 2, critical = 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.

Any help is appreciated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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