Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

216
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda