【问题标题】:Summing values by matching property values通过匹配属性值求和值
【发布时间】:2021-12-21 13:57:19
【问题描述】:

我需要通过匹配对象属性来求和值的帮助。例如,一个对象可能如下所示:

{
  proc_nm: 'postgres-loader',
  count: '290',
  date_trunc: '2021-11-03T14:00:00.000Z'
}

我想对具有匹配属性的对象的count 值求和,例如NAMES('postgres-loader' etc)

如何做到这一点?

【问题讨论】:

  • 你可以filter 然后reduce: data.filter((d) => d.proc_nm == 'postgres-loader').reduce((prev, cur) => prev += cur.count, 0) 假设data 是问题中对象的数组。
  • 您要计算匹配的数组条目的数量,还是要将这些值相加?
  • "a JSON sn-p":那不是 JSON。请阅读json标签的使用说明,并重新考虑如何使用JSON字。

标签: javascript arrays algorithm


【解决方案1】:
const names = ['postgres-loader', 'postgres-connector', 'postgres-reader'];

const variables = [
    {
        proc_nm: 'postgres-connector',
        count: '290',
        date_trunc: '2021-11-03T14:00:00.000Z'
    },
    {
        proc_nm: 'postgres-loader',
        count: '290',
        date_trunc: '2021-11-03T14:00:00.000Z'
    },
    {
        proc_nm: 'postgres-writer',
        count: '290',
        date_trunc: '2021-11-03T14:00:00.000Z'
    }
];

let count = 0;

variables.forEach((variable) => {
    if (names.includes(variable.proc_nm)) count++;
});

console.log(count);

【讨论】:

    【解决方案2】:

    使用forEach 是一种选择。

    const variables = [
        {
            proc_nm: 'postgres-connector',
            count: '290',
            date_trunc: '2021-11-03T14:00:00.000Z'
        },
        {
            proc_nm: 'postgres-loader',
            count: '290',
            date_trunc: '2021-11-03T14:00:00.000Z'
        },
        {
            proc_nm: 'postgres-writer',
            count: '290',
            date_trunc: '2021-11-03T14:00:00.000Z'
        }
    ];
    
    let count = 0;
    variables.forEach(x => {
        count += x.proc_nm === 'postgres-connector';
    });
    
    console.log(count);

    Array.prototype.forEach - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

    如果您想按特定的proc_nm 分组并计算有多少对象具有相同的值,您可以使用reduce

    const variables = [
        {
            proc_nm: 'postgres-connector',
            count: '290',
            date_trunc: '2021-11-03T14:00:00.000Z'
        },
        {
            proc_nm: 'postgres-loader',
            count: '290',
            date_trunc: '2021-11-03T14:00:00.000Z'
        },
        {
            proc_nm: 'postgres-writer',
            count: '290',
            date_trunc: '2021-11-03T14:00:00.000Z'
        },
        {
            proc_nm: 'postgres-writer',
            count: '290',
            date_trunc: '2021-11-03T14:00:00.000Z'
        }
    ];
    
    var result = [];
    variables.reduce((res, value) => {
      if (!res[value.proc_nm]) {
        res[value.proc_nm] = { proc_nm: value.proc_nm, count: 0 };
        result.push(res[value.proc_nm])
      }
      res[value.proc_nm].count += 1;
      return res;
    }, {});
    
    console.log(result)

    【讨论】:

      猜你喜欢
      • 2018-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-16
      • 1970-01-01
      • 2021-02-18
      相关资源
      最近更新 更多