【问题标题】:How to get a sum of an array column field in an angular array 9.1.9?如何在角度数组 9.1.9 中获取数组列字段的总和?
【发布时间】:2024-05-19 18:55:02
【问题描述】:

我正在使用 Angular 9.1.9 和 Html。我想对嵌套数组字段求和或求和并在行中显示。

我有一个包含多个字段的数组列表('adherant')。我想对数组列字段 {{ Montant total }} 求和,并显示在像 photo.using Angular 9 和 Html 这样的文本总数中。我想对数组列字段 {{ Montant total }} 求和,并像照片一样显示在文本总数中。 如果有管道方法,或过滤器!

文件

interface adher {
  four?: string;
  mont: number;
  nombr: number;
  monmois: number;
  }

const adherant: adher[] = [
  {
    four: 'Russia',
    mont: 444,
    nombr: 17075200,
    monmois: 146989754
  },
  {
    four: 'Russasia',
    mont: 444,
    nombr: 17075200,
    monmois: 146989754
  },{
    four: 'ssss',
    mont: 444,
    nombr: 17075200,
    monmois: 146989754
  },{
    four: 'Russddddia',
    mont: 444,
    nombr: 17075200,
    monmois: 146989754
  },{
    four: 'sdsd',
    mont: 444,
    nombr: 17075200,
    monmois: 146989754
  },{
    four: 'Russcxcxcxia',
    mont: 444,
    nombr: 17075200,
    monmois: 146989754
  }
];
<table class="table table-striped">

                        <thead>

                            <tr>
                                <th scope="col">Fournisseur</th>
                                <th scope="col">Montant totale</th>
                                <th scope="col">Nombre de mois</th>
                                <th scope="col">Montant par mois</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr *ngFor="let adh of adherant |filter:search">
                                <th scope="row">{{ adh.four }}</th>
                                <td>
                                    {{ adh.mont }}
                                </td>
                                <td>{{ adh.nombr | number}}</td>
                                <td>{{ adh.monmois | number }}</td>
                            </tr>
                        </tbody>
                    </table>

【问题讨论】:

  • 您可以将数据从过滤器管道发送到通用服务并从通用服务中获取总数
  • 我该怎么做
  • 你能创建 stackblitz 演示吗?
  • 好的,我会试试的
  • 在下面查看我的答案

标签: javascript html angular typescript


【解决方案1】:

在组件声明中:-

公开总数;

this.total= this.adherant.reduce((prev,next)=&gt;prev+next.mont,0);

在模板中像 {{total}} 一样使用它

【讨论】:

  • 尝试控制台日志记录总数
  • 我忘了加上这个。也这样做
最近更新 更多