typescript 如何在表角中求和

mcdcgff0  于 2023-08-07  发布在  TypeScript
关注(0)|答案(1)|浏览(97)

我想在我的表中总结这些数字:


的数据
我需要做一个过滤器还是需要ngFor?我知道计算没有意义。我试探着:

<th>
    <i>Sum:{{ this.invoices?.sum | number }}</i>
 </th>

字符串
ts:

invoices: any;

  calcSum = ( invoice: any, sum:number): string => {
        if (!invoice||!sum) { 
            return '0';
        }
        return (this.invoices.sum + this.invoices.sum); 
        
    }


接口:

export interface InvoiceResponse{
    paid: boolean,
    paidAmount: number,
    client: string,
    number: number,
    total:number,
    taxTotal: number,
    sum: number,
    currencyRate: string,
    currencyRateAtIssueDate: string,
    issuedAtPlace: string,
    issuedAt: string,
    deadlineAt: string


}

jv4diomz

jv4diomz1#

我的天

calcSum = this.invoices.map((i) => i.sum).reduce((a, b) => a + b);

字符串
联系我们

<th>
  <i>Sum:{{ calcSum  }}</i>
</th>

snippet

const invoices = [{sum:1}, {sum:2}, {sum:3}];

const calcSum = invoices.map((i) => i.sum).reduce((a, b) => a + b);

console.log(calcSum);


更多信息和一些例子可在以下文章中找到:

相关问题