Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Fastest way to sum specific value in array of nested objects

I have currently two objects in my cartProducts array and the size will grow more in future. I am trying to calculate the sum of all items prices in products for all the objects. I have tried to use forEach but it seems to work slowly. How can I calculate the sum of prices in a faster way? I saw that using reduce is fine but as there are a lot of nested objects, I cannot figure out how to use it.

{
    "cartProducts": [
        {
            "id": "1",
            "products": [
                {
                    "id": "123",
                    "supplier": "Milkinis",
                    "items": [
                        {
                            "id": "14553",
                            "name": "eggs",
                            "price": "1.56",
                        },
                        {
                            "id": "14554",
                            "name": "flour",
                            "price": "1.98",
                        },
                    ]
                },
                {
                    "id": "124",
                    "supplier": "Lindy",
                    "items": [
                        {
                            "id": "14553",
                            "name": "chocolate",
                            "price": "4.5",
                        },
                    ]
                }
            ]
        },
        {
            "id": "2",
            "products": [
                {
                    "id": "125",
                    "supplier": "Wisk",
                    "items": [
                        {
                            "id": "14553",
                            "name": "water",
                            "price": "3.56",
                        },
                    ]
                }
            ]
        },
    ]
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here is the code using reduce

sum = obj.cartProducts.reduce((acc, cart) => {
  return acc + cart.products.reduce((acc, product) => {
    return acc + product.items.reduce((acc, item) => {
      return acc + parseFloat(item.price);
    }, 0);
  }, 0);
}, 0);

console.log(sum);
<script>
  var obj = {
    "cartProducts": [{
        "id": "1",
        "products": [{
            "id": "123",
            "supplier": "Milkinis",
            "items": [{
                "id": "14553",
                "name": "eggs",
                "price": "1.56",
              },
              {
                "id": "14554",
                "name": "flour",
                "price": "1.98",
              },
            ]
          },
          {
            "id": "124",
            "supplier": "Lindy",
            "items": [{
              "id": "14553",
              "name": "chocolate",
              "price": "4.5",
            }, ]
          }
        ]
      },
      {
        "id": "2",
        "products": [{
          "id": "125",
          "supplier": "Wisk",
          "items": [{
            "id": "14553",
            "name": "water",
            "price": "3.56",
          }, ]
        }]
      },
    ]
  };
</script>

About performance of reduce vs loop look here Javascript performance: reduce() vs for-loop

Thanks

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading