如何仅显示一次和';总计';对于使用Angular7的类似单词
从这个循环中,我得到了所有类别的数据如何仅显示一次和';总计';对于使用Angular7的类似单词,angular,typescript,Angular,Typescript,从这个循环中,我得到了所有类别的数据 {{item}} 如在 我想展示一下 我们的价值观(2) 宽(2) 让我介绍一下(3) 第(1)条 等 我只想显示一次,对于类似的单词,显示“total” 你能和我分享一些想法吗?this.items=category.category?.split(',')。reduce((acc,item)=>{ this.items = category.Category?.split(',').reduce((acc, item) => { let val
{{item}}
如在
我想展示一下
- 我们的价值观(2)
- 宽(2)
- 让我介绍一下(3)
- 第(1)条 等
this.items=category.category?.split(',')。reduce((acc,item)=>{
this.items = category.Category?.split(',').reduce((acc, item) => {
let value = acc[item] || 0;
return {...acc, [item]: ++value};
}, {});
<mat-card-actions *ngFor="let item of items | keyvalue" (click)="onFilterByCategory(item)">
<button mat-stroked-button color="primary">{{item.key}}({{item.value})</button> </mat-card-actions>
设值=附件[项目]| | 0;
返回{…acc,[项]:++value};
}, {});
{{item.key}}({{item.value})
就这样做吧
组件技术
categories = ['cat1','cat2','cat3', 'cat2', 'cat1'];
result = {};
this.categories.forEach(i => {
this.result[i] = this.categories.filter(item => item === i).length;
});
component.html
<mat-card-actions *ngFor="let item of categories"
(click)="onFilterByCategory(item)">
<button mat-stroked-button color="primary">{{ item}} {{result[item]}}</button>
</mat-card-actions>
{{item}}{{result[item]}
我刚才给了你一个例子,说明了如何显示重复的
类别的计数。你只需创建一个对象,将每个类别的计数与类别
名称
作为键
,值将是键的计数。在这种情况下,我更喜欢使用Map
let categories = ["CatA", "CatB", "CatA", "CatC"]
this.categoriesMap = new Map<string, number>();
categories.forEach(category => {
this.categoriesMap.set(category, (this.categoriesMap.get(category) || 0) + 1)
});
let categories=[“CatA”、“CatB”、“CatA”、“CatC”]
this.categoriesMap=新映射();
类别。forEach(类别=>{
this.categoriesMap.set(category,(this.categoriesMap.get(category)| | 0)+1)
});
然后您的html将如下所示
<mat-card-actions *ngFor="let item of categoriesMap | keyvalue"
(click)="onFilterByCategory(item)">
<button mat-stroked-button color="primary">{{ item.key}} {{item.value}}</button>
</mat-card-actions>
{{item.key}{{item.value}