【发布时间】:2017-11-26 06:50:53
【问题描述】:
我一直在通过 Ray Villalobos 在 Lynda.com (https://github.com/planetoftheweb/learnangular) 上的一门课程试验这个 GitHub 存储库——它的功能类似于我希望构建的基本网络应用程序,但我最近碰到了一点路障。
在上面链接的那个 repo 中,在 app/component.app.ts 中,有以下数组:
var ARTISTS: Artist[] = [
{
"name": "Barot Bellingham",
"shortname": "Barot_Bellingham",
"reknown": "Royal Academy of Painting and Sculpture",
"bio": "Some bio here."
},
// etc...
]
该数组由管道过滤,如 app/pipe.search.ts 所示:
export class SearchPipe implements PipeTransform {
transform(pipeData, pipeModifier) {
return pipeData.filter((eachItem) => {
return eachItem['name'].toLowerCase().includes(pipeModifier.toLowerCase()) ||
eachItem['reknown'].toLowerCase().includes(pipeModifier.toLowerCase());
});
}
}
这是过滤器输入:
<input class="search-input" [(ngModel)]="field1Filter" placeholder="type in search term here" (click)="showArtist(item); field1Filter=''">
以及过滤结果的代码:
<ul class="artistlist cf" *ngIf="field1Filter">
<li class="artistlist-item cf"
(click)="showArtist(item);"
*ngFor="let item of (artists | search: field1Filter)">
<artist-item class="content" [artist]=item></artist-item>
</li>
</ul>
<artist-details *ngIf="currentArtist" [artist]="currentArtist"></artist-details>
这一切都很完美,但是,在我的项目中,我需要包含三个嵌套数组,并且能够根据这些数组中的值进行过滤。我需要的数组示例如下所示:
var ARTISTS: Artist[] = [
{
"name": "Barot Bellingham",
"shortname": "Barot_Bellingham",
"reknown": "Royal Academy of Painting and Sculpture",
"bio": "Some bio here...",
"friends": [
"James",
"Harry",
"Bob",
"Liz",
"Kate",
"Jesse"
],
"emails": [
"bb@this.com",
"aa@this.com"
],
"car": [
"honda",
"scion",
"aston martin"
]
},
// etc...
]
因此,我希望按“Harry”进行过滤,仅显示“姓名”、“知名”、“朋友”、“电子邮件”或“汽车”中包含“哈利”的对象。这可能吗?如果可以,我该如何编辑管道过滤器来做到这一点?谢谢!!
(总的来说,我在 Angular 和 JS 方面很菜,所以如果我使用了不正确的术语或忽略/误解了一些基本的东西,我想提前道歉。)
【问题讨论】:
标签: javascript arrays angular filter pipe