angular - 从视图中删除重复值 - Angular
问题描述
下面是我想要过滤的重复字段,只显示一个而不是两个:
"release_dates":[ {" certificate":"PG-13","iso_639_1 ":"","note":"碲化物电影节","release_date":"2018-08-31T00:00:00.000Z", “类型”:1},
{"认证":"PG-13","iso_639_1" :"","note":"","release_date":"2018-09-28T00:00:00.000Z","type":2}]} ]}
下面的组件显示来自上述 json 的两条记录:
import { Component, OnInit } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
import { ActivatedRoute } from '@angular/router';
import { MoviesService } from '../movies.service';
@Component({
selector: 'app-movie',
templateUrl: './movie.component.html',
styleUrls: ['./movie.component.css']
})
export class MovieComponent implements OnInit {
movie: Object;
certification: Array<Object>;
video: Object;
constructor(
private _moviesServices: MoviesService,
private router: ActivatedRoute
) {
}
ngOnInit() {
this.router.params.subscribe((params) => {
const id = params['id'];
this._moviesServices.getMovie(id).subscribe(movie => {
this.movie = movie;
});
this._moviesServices.getCertification(id).subscribe((res: any) => {
const usCertifications = res.results.filter((result: any) => {
return result.iso_3166_1 === "US";
// return this.certification === result.certificationAll;
});
this.certification = usCertifications;
});
})
}
}
html:
<div class="mr-3" *ngIf="certification"><span *ngFor="let cert of certification">
<span *ngFor="let release of cert.release_dates">
<span class="badge badge-pill badge-warning" *ngIf="release.certification">{{release.certification}}</span>
</span>
</span>
</div>
解决方案
您可以添加一个函数来删除组件中的重复项,或者使用管道做同样的事情类似于这个
result:any=[];
removeDuplicates(): any{
this.certification.forEach((item)=>{
if(this.result.indexOf(item) < 0) {
this.result.push(item);
}
});
return this.result;
}
然后在模板中调用它
<div class="mr-3" *ngIf="certification"><span *ngFor="let cert of removeDuplicates(certification)">
<span *ngFor="let release of cert.release_dates">
<span class="badge badge-pill badge-warning" *ngIf="release.certification">{{release.certification}}</span>
</span>
</span>
</div>
推荐阅读
- java - JavaFx tableview 和 tablecolumn 中的两个不同的类
- xamarin.forms - EF Database.EnsureDeleted() 和 Database.EnsureCreated() 方法上的 System.Reflection.TargetParameterCountException
- asp.net-core - 如何验证嵌入 OKTA 的 PowerBI
- bash - 我的 max 函数抛出错误,即使它与我的 min 函数相同但翻转了,找不到错误?
- r - R - 根据匹配的名称逐个元素地组合任意列表
- ios - 如何根据远程通知类型将用户重定向到各种屏幕
- c# - 无法在 Mac OSX 上启动 picoe/eto 应用程序
- laravel - 手动传递外键值
- javascript - 这两种在Javascript中创建构造函数的方式有什么区别?
- c# - 如何动态绑定集合中的枚举值?(WPF)