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