如何计算几个总金额的总和?

How to calculate the sum of several total amounts?

我有一个对象数组,我在其中创建需要在 html 中显示的属性。这是数组中对象之一的结构

 {
    "functional_id": "201911291131250012400000SD4AYAA1",
    "transactions": [
      {
        "quantity": 2,
        "price": 140,
        "item": {
          "name": "Carton de 10 coffrets",
          "description": "+ 2 recharges d'argile offertes",
          "product": {
            "name": "Coffret empreinte rouge"
          }
        },
        "amount": 280
      },
      {
        "quantity": 2,
        "price": 17,
        "item": {
          "name": "1 lanterne d'accueil + 1 accroche porte",
          "product": {
            "name": "Lanterne d'accueil",
            "description": "Lors d'une euthanasie, cette affichette verticale auto-éclairée par bougie LED, est disposée à l’accueil. Elle montre l’importance de ce moment pour votre clinique. <br /> Les accroches porte déposés sur les poignées des salles de consultation invitent au calme."
          }
        },
        "amount": 34
      },
      {
        "quantity": 1,
        "price": 0,
        "item": {
          "name": "Petit modèle",
          "description": "Par 25",
          "product": {
            "name": "Sacs blancs",
            "description": "Pour les crémations Plurielles"
          }
        },
        "amount": 0
      },
      {
        "quantity": 1,
        "price": 0,
        "item": {
          "name": "Moyen modèle",
          "description": "Par 20",
          "product": {
            "name": "Sacs blancs",
            "description": "Pour les crémations Plurielles"
          }
        },
        "amount": 0
      },
      {
        "quantity": 1,
        "price": 0,
        "item": {
          "name": "Grand modèle",
          "description": "Par 10",
          "product": {
            "name": "Sacs blancs",
            "description": "Pour les crémations Plurielles"
          }
        },
        "amount": 0
      },
      {
        "quantity": 2,
        "price": 0,
        "item": {
          "product": {
            "name": "Carnet de conventions"
          }
        },
        "amount": 0
      }
    ],
    "date": "29/11/2019",
    "order_number": "113125"
  }

此时我为每个'transaction'创建一个属性'amount'来计算每个产品的购买总额

    public inicializeData() {

        this.loaderService.eLoader.emit(true);
        this.cartsSubscription = this.cartsService.getCarts().subscribe(carts => {
            this.orders = carts;
            this.orders.forEach(cart => {
                const day = cart.functional_id.substring(6, 8);
                const month = cart.functional_id.substring(4, 6);
                const year = cart.functional_id.substring(0, 4);
                cart.date = day + '/' + month + '/' + year;
                cart.order_number = cart.functional_id.substring(8, 14);
                cart.transactions.forEach(item => {
                    item.amount = item.quantity * item.price;
                });
            });
            this.load = true;
            this.loaderService.eLoader.emit(false);
        });
    }



问题是我还需要所有交易的总金额,但我无法计算这个总金额。 首先,我声明一个值为 0 的 class 变量,然后在计算 'item.amount' 时将它们全部加起来。

public amount = 0;

 public inicializeData() {

        this.loaderService.eLoader.emit(true);
        this.cartsSubscription = this.cartsService.getCarts().subscribe(carts => {
            this.orders = carts;
            this.orders.forEach(cart => {
                const day = cart.functional_id.substring(6, 8);
                const month = cart.functional_id.substring(4, 6);
                const year = cart.functional_id.substring(0, 4);
                cart.date = day + '/' + month + '/' + year;
                cart.order_number = cart.functional_id.substring(8, 14);
                cart.transactions.forEach(item => {
                    item.amount = item.quantity * item.price;
                    this.amount += item.amount;
                });
            });
            this.load = true;
            this.loaderService.eLoader.emit(false);
        });
    }



我看到我得到的是所有交易的总和。这是不希望的。

我知道这是范围问题。但是我也试过在最后一个forEach里面声明变量,但是也没用。

    public inicializeData() {

        this.loaderService.eLoader.emit(true);
        this.cartsSubscription = this.cartsService.getCarts().subscribe(carts => {
            this.orders = carts;
            this.orders.forEach(cart => {
                const day = cart.functional_id.substring(6, 8);
                const month = cart.functional_id.substring(4, 6);
                const year = cart.functional_id.substring(0, 4);
                cart.date = day + '/' + month + '/' + year;
                cart.order_number = cart.functional_id.substring(8, 14);
                cart.transactions.forEach(item => {
                    item.amount = item.quantity * item.price;
                    let overallAmount = 0;
                    overallAmount += item.amount;
                });
            });
            this.load = true;
            this.loaderService.eLoader.emit(false);
            console.log(JSON.stringify(this.orders));
        });
    }

我需要的是,在示例对象中,我可以创建一个 属性,它是所有 item.amount 的总和(在这种情况下,它将是 overallAmount = 314)。

有人让我看到我的错误。非常感谢您

在这一行this.amount += item.amount; 尝试做

const cal = item.quantity * item.price;
            item.amount=cal
            this.amount += cal;

var amount=[]
const cal = item.quantity * item.price;
                item.amount=cal
                var.push(cal)
amount=amount.reduce((a,b)=>{return a+b})

如果您希望向您的对象添加 属性 交易金额,您可以使用此

t=0
x= cart.transactions.filter(x=>cart['transac-amount']=t+=x.amount)
console.log(cart)

它将向您的对象添加交易金额

首先,我要感谢所有提供帮助的人。多亏了你给我的方法,我找到了解决方案。我最终在对象 'cart' 中创建了一个 属性,我在其中输入了每个项目的总和值。 我的主要错误是我在同一范围内执行了所有操作,或者我试图将它累积在一个 class 变量中,该变量返回所有项目的总数。 在这里,我为您提供完美的解决方案。 再次感谢您的帮助

    public inicializeData() {

        this.loaderService.eLoader.emit(true);
        this.cartsSubscription = this.cartsService.getCarts().subscribe(carts => {
            this.orders = carts;
            this.orders.forEach(cart => {
                const day = cart.functional_id.substring(6, 8);
                const month = cart.functional_id.substring(4, 6);
                const year = cart.functional_id.substring(0, 4);
                cart.date = day + '/' + month + '/' + year;
                cart.order_number = cart.functional_id.substring(8, 14);
                const cash = [];
                cart.overallAmount = 0;
                cart.transactions.forEach(item => {
                    item.amount = item.quantity * item.price;
                    cash.push(item.amount);
                    cart.overallAmount = cash.reduce((a, b) => a + b, 0);
                });
            });
            this.load = true;
            this.loaderService.eLoader.emit(false);
        });
    }