Home > Software engineering >  Fastest way to sum specific value in array of nested objects
Fastest way to sum specific value in array of nested objects

Time:08-15

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",
                        },
                    ]
                }
            ]
        },
    ]
}

CodePudding user response:

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

CodePudding user response:

If you have no quantity, perhaps this would be fast enough

const cartStr = JSON.stringify(cart)
const prices = [...cartStr.matchAll(/"price":"(\d \.\d )"/g)]
  .reduce((acc, [_,match]) => acc  =  match,0)
console.log(prices)
<script>
const cart = {
    "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>

  • Related