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

90
Views
JS: manipulación de datos en una matriz de objetos con propiedad que contiene una matriz

Matriz de ejemplo -

 const arr = [ { charge: [ { id: '1', qty: 10 }, { id: '2', qty: 20 } ], totalCharge: 100, qtySum: 50 }, { charge: [ { id: '3', qty: 30 }, { id: '4', qty: 40 } ], totalCharge: 70, qtySum: 100 }, ]

Las acciones matemáticas - (qty * totalCharge) / qtySum

Salida (no números reales) -

 [ { id: '1', calcQty: 300, }, { id: '2', calcQty: 250, }, { id: '3', calcQty: 300 }, { id: '4', calcQty: 400 } ]

Lo que no pude entender cómo hacer es separar la charge y solo luego hacer los cálculos que necesito. Debido a que necesito usar el mismo totalCharge y qtySum cada vez en función de cuántos órganos hay en el campo de charge , me encantaría obtener ayuda.

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

0

Puede usar una combinación de map y flat .

 const arr = [ { charge: [ { id: "1", qty: 10 }, { id: "2", qty: 20 }, ], totalCharge: 100, qtySum: 50, }, { charge: [ { id: "3", qty: 30 }, { id: "4", qty: 40 }, ], totalCharge: 70, qtySum: 100, }, ]; const res = arr .map(({ charge, totalCharge, qtySum }) => charge.map(({ id, qty, stySum }) => ({ id, calcQty: (totalCharge * qty) / qtySum, })) ) .flat(); console.log(res);

about 4 years ago · Juan Pablo Isaza Report

0

Puede usar forEach dos veces para realizar la acción matemática.

Prueba como a continuación.

 const arr = [ { charge: [ { id: "1", qty: 10 }, { id: "2", qty: 20 }, ], totalCharge: 100, qtySum: 50, }, { charge: [ { id: "3", qty: 30 }, { id: "4", qty: 40 }, ], totalCharge: 70, qtySum: 100, }, ]; const output = []; arr.forEach(({ charge, totalCharge, qtySum }) => { charge.forEach(({ id, qty }) => { output.push({ id, calcQty: (qty * totalCharge) / qtySum }); }); }); console.log(output);

about 4 years ago · Juan Pablo Isaza Report

0

Podría mapear matrices anidadas y obtener un resultado plano.

 const array = [{ charge: [{ id: '1', qty: 10 }, { id: '2', qty: 20 }], totalCharge: 100, qtySum: 50 }, { charge: [{ id: '3', qty: 30 }, { id: '4', qty: 40 }], totalCharge: 70, qtySum: 100 }], result = array.flatMap(({ charge, totalCharge, qtySum }) => charge.map(({ id, qty }) => ({ id, calcQty: qty * totalCharge / qtySum })) ); console.log(result);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

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!