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

109
Views
Sumar valores haciendo coincidir valores de propiedad

Necesito ayuda para sumar valores haciendo coincidir las propiedades de los objetos. Entonces, por ejemplo, un objeto podría verse así:

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

Me gustaría sumar los valores de count de los objetos que tienen una propiedad coincidente, como NAMES('postgres-loader' etc) .

¿Cómo se puede hacer esto?

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

0

Usar forEach es una opción.

 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

Si desea agrupar por el proc_nm específico y contar cuántos objetos tienen el mismo valor, puede usar 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!