Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

117
Views
Summing values by matching property values

I need help summing values by matching object properties. So for example, one object might look like this:

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

I would like to sum the count values of the objects that have a matching property, like with NAMES('postgres-loader' etc).

How can this be done?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Using forEach is an option.

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

If you want to group by the specific proc_nm and count how many objects have the same value you can use 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)

about 4 years ago · Juan Pablo Isaza Report

0

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);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!