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

122
Views
Creando una matriz de objetos con suma acumulativa en javascript

Tengo esta matriz original que necesito transformar en la que se llama original.

 const data = [ { end_date: "2020-01-31" invoices_count: 22 month: "2020-01" start_date: "2020-01-01" total_margin: 1000 total_revenue: 1000 }, { end_date: "2020-02-29" invoices_count: 14 month: "2020-02" start_date: "2020-02-01" total_margin: 2000 total_revenue: 2000 }]

necesito acumular el margen_total y el ingreso_total a lo largo del tiempo, de modo que el resultado sea igual al mes anterior + el valor del mes actual, por lo que la matriz resultante se verá así:

 const result = [ { end_date: "2020-01-31" invoices_count: 22 month: "2020-01" start_date: "2020-01-01" total_margin: 1000 total_revenue: 1000 }, { end_date: "2020-02-29" invoices_count: 14 month: "2020-02" start_date: "2020-02-01" total_margin: 3000 total_revenue: 3000 }]

Escribí esta función para hacer esto, pero también está modificando el primer valor en la matriz (total_margin & total_margin en el índice 0), y los números realmente no suman el valor correcto, no estoy seguro de lo que estoy haciendo equivocado

 export const getCumulativeValueMonth = (data: MonthlyRevenue[]): MonthlyRevenue[] => { const orderedData = _.orderBy(data, (o) => Date.parse(o.start_date), ['asc']); const accumulated = orderedData.map((x, i) => ({ start_date: x.start_date, end_date: x.end_date, invoices_count: x.invoices_count, month: x.month, total_margin: data .slice(0, i + 1) .map(({ total_margin }) => total_margin) .reduce((z, y) => z + y), total_revenue: data .slice(0, i + 1) .map(({ total_revenue }) => total_revenue) .reduce((z, y) => z + y), })); return accumulated; };

¿Alguna idea de lo que hice mal?

¡Gracias!

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

0

Está cortando los data de la matriz original, pero necesita tomar los accumulated ordenados.


Para un enfoque más corto, puede cerrar las sumas acumulativas deseadas y agregar los valores y devolver un nuevo objeto.

 const original = [{ end_date: "2020-01-31", invoices_count: 22, month: "2020-01", start_date: "2020-01-01", total_margin: 1000, total_revenue: 1000 }, { end_date: "2020-02-29", invoices_count: 14, month: "2020-02", start_date: "2020-02-01", total_margin: 2000, total_revenue: 2000 }], result = original.map(((total_margin, total_revenue) => o => { total_margin += o.total_margin; total_revenue += o.total_revenue; return { ...o, total_margin, total_revenue }; })(0, 0)); console.log(result);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

Con llaves diferentes.

 const original = [{ end_date: "2020-01-31", invoices_count: 22, month: "2020-01", start_date: "2020-01-01", total_margin: 1000, total_revenue: 1000 }, { end_date: "2020-02-29", invoices_count: 14, month: "2020-02", start_date: "2020-02-01", total_margin: 2000, total_revenue: 2000 }], result = original.map(((margin, revenue) => ({ total_margin, total_revenue, ...o }) => { margin += total_margin; revenue += total_revenue; return { ...o, margin, revenue }; } )(0, 0) ); console.log(result);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Report

0

res = data.reduce((acc, it) => { if (acc.length > 0) { let last = acc[acc.length-1]; acc.push({...it, total_margin: it.total_margin + last.total_margin, total_revenue: it.total_revenue + last.total_revenue}); } else { acc.push(it); } return acc; }, []);
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!