7

I am using Pug template engine with Node + Express app.

I need some calculation in the Pug file. For example, I have an array of object and I have to print the sum of all object's amount field and need to show all amount in table.

for that, I am using each loop available in Pug.

I am trying like this :

   div
    each discount in el.Discounts
     if trxn.category != category
      var discountAmount = discount.amount * -1
      var distTotal = distTotal + discount.amount
      p= distTotal

But it is not working, I want to declare and update the inline variable.

How can I achieve this?

Thanks.

3
  • What is p? What is the output you are getting? What are you expecting? Please share some data. Aug 4, 2018 at 12:05
  • P is HTML's paragraph tag. I am not getting any error but nor getting the result. Aug 4, 2018 at 12:07
  • Shouldn't p be outside if statement? and also just define discountAmount and distTotal before each iterator as 0; Aug 4, 2018 at 12:25

1 Answer 1

13

In front of var you should write "-"

div
    each discount in el.Discounts
     if trxn.category != category
      - var discountAmount = discount.amount * -1
      - var distTotal = distTotal + discount.amount
      p= distTotal

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.