Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reduce() in function to calculate total of products

{
  "orders" : [ null, {
    "comment" : "Bitte, Lassen Sie die Pizza geschnitten.",
    "date" : "2018-06-01 07:22:10",
    "item" : [ {
      "name" : "Tomatensuppe",
      "price" : 3.9,
      "quantity" : 2,
      "size" : ""
    }, {
      "name" : "Estragoncremesuppe",
      "price" : 4.5,
      "quantity" : 1,
      "size" : ""
    } ]
  }, {
    "comment" : "Geben Sie Brot dazu",
    "date" : "2018-03-19 15:22:20",
    "item" : [ {
      "name" : "Minestrone",
      "price" : 3.9,
      "quantity" : 3,
      "size" : ""
    }, {
      "name" : "Tomatensuppe",
      "price" : 3.9,
      "quantity" : 2,
      "size" : ""
    } ]
  } ]
}

I would like to calculate a total by orders from this json array.

methods:
    grandTotal: function(i) {
      return i.price.reduce((i) => {
        sum + i.price * i.quantity 
          return sum;
      }, 0)
    },

In grandTotal function I don't get what's wrong with this approach.

<td><ul><li v-for="i in order.item"> {{i.name}} ({{i.size}}) {{i.quantity}} x unit price: {{i.price}} subtotal by item: {{product(i)}}
        <li class="comment-order"><b>Bemerkung: </b>{{order.comment}}</li>
        <li>Total: {{sum}} </li>
        </ul></td>

I want to calculate the sum of the row totals, but I don't understand what is wrong with my function.

like image 689
robertkovacs Avatar asked Jan 28 '26 19:01

robertkovacs


1 Answers

getTotalQuantity(items) {
    return items.reduce((total, item) => item.quantity + total, 0)
  }

items here is an array of objects with qunatity value

like image 196
Baabar Ali Avatar answered Jan 30 '26 10:01

Baabar Ali



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!