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

214
Views
¿Calcular el elemento anterior como entrada al siguiente elemento de la matriz en JS?

Tengo el siguiente código para calcular la dinámica del valor de las existencias en función del valor calculado anterior

y también el único último valor del elemento que tenemos el campo stock actual que será útil para calcular el stock dinámico

 this.stockList.map(function(product, index) { product.Data.map(function(attribute, currentIndex) { if (currentIndex == 0) { attribute.stock = attribute.currentStock; } else { const requestVal = (attribute.qty * attribute.unit) + attribute.stock; // here i need to have a previous stock value console.log("requestVal", attribute); attribute.stock = requestVal; } }); });

mi matriz esperada de muestra:

 [ { Product: ABC Data: [ { "billDate": "1-apr-2016", "unit": 2, "Qty": 4, "Amount": 4500, "currentStock": 10 }, { "billDate": "1-may-2016", "unit": 3, "Qty": 2, "Amount": 4500, "stock": (2 * 3) + 10 = 16 }, { "billDate": "1-may-2016", "unit": 1, "Qty": 2, "Amount": 4500, "stock": (2 * 1) + 16 = 18 }, ], }, ]
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Observación/Sugerencia :

  • Debería ser attribute.Qty en lugar de attribute.qty
  • En la segunda iteración, puede usar product.Data[currentIndex - 1].stock para obtener el valor de stock del objeto anterior.

Prueba esto :

 const stockList = [ { Product: 'ABC', Data: [ { "billDate": "1-apr-2016", "unit": 2, "Qty": 4, "Amount": 4500, "currentStock": 10 }, { "billDate": "1-may-2016", "unit": 3, "Qty": 2, "Amount": 4500 }, { "billDate": "1-may-2016", "unit": 1, "Qty": 2, "Amount": 4500 }, ], }, ]; stockList.forEach(function(product, index) { product.Data.forEach(function(attribute, currentIndex) { if (currentIndex === 0) { attribute.stock = attribute.currentStock; } else { const requestVal = (attribute.Qty * attribute.unit) + product.Data[currentIndex - 1].stock; attribute.stock = requestVal; } }); }); console.log(stockList)

about 4 years ago · Santiago Trujillo 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!