简体   繁体   English

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

[英]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遍历队列,将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)

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.

相关问题 按另一个对象的值过滤对象数组 - Filter array of objects by another object's values 过滤基于 object 的对象数组,其中数组作为其属性值在反应 - filter an array of objects based on an object with an array as it's property value in react 如何根据公共属性的值从另一个对象数组的所有元素中过滤一个对象数组 - How to filter an arrayof objects from all elements of another array of object on values of a common property 按 object 属性值过滤数组 - Filter array by object property values 对象数组到属性值的对象 - Array of objects to object of property values 是否可以向对象数组中的过滤对象添加其他属性? - Is it possible to add additional property to to filtered objects from an array of objects? 按另一个对象数组的属性按整数过滤数组 - filter array by integers by property of another array of objects 如果一个属性等于另一个 object 的属性,如何过滤位于对象数组中的 object 的少数属性 - How to filter few properties of an object which is in Array of objects if one property equals property from another object javascript通过另一个对象数组内的属性检查添加或替换对象 - javascript add or replace an object by property check inside another array of objects JS:如何过滤对象数组,在第一个 object 中添加一个键,其值为过滤元素的 rest? - JS: How to filter an array of objects, add to the first object a key with values of the rest of filtered elements?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM