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

215
Views
How to get accumulated price in a nested array using reduce in React.js

I have an array of objects (cart products). In each object, there is an array of "ShopCartList", and productCartList another array of objects. How can I accumulate the price of all "Price" for each product? The cart API enter image description here

The goal is to get the total price and also total price when adding products from a different shop.

{
  "CartID": "610a20c7d51ba524b7f949cd",
  "ShopCartList": [
    {
      "ShopID": "610a29e1d51ba524b7f949d0",
      "productCartList": [
        {
          "ProductID": "610a33f7d51ba524b7f949df",
          "VariationSKU": "062101403N",
          "Price": 2385.96,
          "Count": 1,
          "Attribute": {
            "Weight": "8.29",
            "Diameter": "5.40"
          },
          "CreatedDate": "2021-11-15T06:43:36.482Z",
          "CreatedBy": "610a20c7d51ba524b7f949cd",
          "LastUpdatedDate": "2021-11-15T06:43:36.482Z",
          "LastUpdatedBy": "610a20c7d51ba524b7f949cd"
        }
      ]
    }
  ]
}

it shows an error of undefined.

enter image description here

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

0

This is how you can reduce to get the sum of a certain key from an array of objects.

ProductCartList.reduce((a,b) => {
  return a + b["Price"]
},0);

The above code returns the total sum of prices of products from a single productCartList array. If you have multiple items inside your ShopCartList array and want to get the sum of all the prices from all the ProductCartLists, you can use reduce on the ShopCartList array like so

ShopCartList.reduce((a,b) => {
  return a + b["ProductCartList"].reduce((a,b) => return a + b["Price"]);
},0);

The above code get adds the sum of all the prices from all the different product carts.

Let me know if this helps!

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!