简体   繁体   English

如何计算和显示来自 javascript object 的字段总和

[英]How to calculate and display sum of field from javascript object

I have an object, where I need to display name.我有一个 object,我需要在其中显示名称。 and the total expense amount of respective person.以及相关人员的总费用金额。

I am able to display name by using ngfor .我可以使用ngfor显示名称。

But how to display the sum of respective amount.但是如何显示各个金额的总和。 Like for John it should display 50 and for steve it should display 170.就像 John 应该显示 50 而 steve 应该显示 170。

Can anyone please help me how to do this.谁能帮助我如何做到这一点。

data = [
  {
    'id'  : 1,
    'name': john,
    'expenselist' = [
      {
        'expenseid': 1,
        'amount'   : 20
      },
      {
        'expenseid': 2,
        'amount'   : 30
      },
    ]
  },
  {
    'id'  : 2,
    'name': steve,
    'expenselist' = [
      {
        'expenseid': 1,
        'amount'   : 80
      },
      {
        'expenseid': 2,
        'amount'   : 90
      },
    ]
  }
]

<ion-label *ngFor="let data of data;">
    {{data.name}}: 
    Total Expense Amount:
</ion-label>

I don't know how to use ngfor.我不知道如何使用 ngfor。 but this is how I would do it in normal JavaScript, I hope it will give an idea.但这就是我在普通 JavaScript 中的做法,我希望它能给出一个想法。

data.forEach(person => {
 console.log(person.name)
 expense_amount = 0
 person.expenselist.forEach(expense => {
  expense_amout = expense_amount + expense.mount
  });
 console.log(expense_amount);
});

Try to make a function to get Total expense amount and it will work.尝试制作 function 以获得总费用金额,它将起作用。

In HTML,在 HTML 中,

  <div *ngFor="let person of data;">
      {{person.name}} : Total Expense Amount : {{getTotalExpenseAmount(person)}}
  </div>

In ts file, add this function,在 ts 文件中,添加这个 function,

getTotalExpenseAmount(data) {
    return data.expenselist.reduce(
      (total, current) => total + current.amount,
      0
    );
  }

use reduce使用减少

const mutatedData = data.reduce((acc, value)=>{
    acc.push({
        name: value.name,
        amount: value.expenselist.reduce((acc, value)=>acc+value.amount, 0)
    })
}, [])

the use mutatedData inside your angular code to produce result在 angular 代码中使用 mutatedData 产生结果

<ion-label *ngFor="let data of mutatedData;">
  {{data.name}}  : 
  Total Expense Amount : {{data.amount}}
</ion-label>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何计算数组中对象键的总和--javascript - How to calculate sum of object keys in array - javascript 在 javascript object 中,如果标签存在于另一个字段中,如何对一个字段的值求和? - In a javascript object, how to sum values from one field if a tag exists in another field? 从Java表单中计算输入并显示在消息字段中 - Calculate inputs from form in Javascript and display in message field 选择字段中要显示在另一个文本字段中的值的总和(javascript) - Sum of values from select fields to display in another text field (javascript) 从属性JavaScript计算总和 - calculate sum from attribute JavaScript 如何将输入字段值相加并使用Javascript将其显示到html表中? - How to sum the input field values and display it into html Table using Javascript? 如何从Javascript / Typescript中的数组对象计算运行总计并使用HTML在每个实例中显示输出? - How to calculate running total from an array object in Javascript/Typescript and display output at every instance using HTML? 如何计算JavaScript中数组的总和 - How to calculate sum of array in JavaScript 如何计算 object 中的数量总和 - how to calculate sum of quantite in an object 如何计算 javascript 或 3sum 中 3 个元素的总和? - how to calculate the sum of 3 element in javascript or 3sum?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM