如何在angular 2中过滤包含其他数组的项目数组?
我正在做材料设计应用和角度2。此应用程序是一个交易卡游戏数据库,用户可以在其中搜索卡。现在数据库是本地的,带有json文件。问题是,我有一个输入,用户在其中写入卡的“种族”,它用于过滤共享该种族的卡的图像网格 卡片类别如下:如何在angular 2中过滤包含其他数组的项目数组?,angular,typescript,angular-material,Angular,Typescript,Angular Material,我正在做材料设计应用和角度2。此应用程序是一个交易卡游戏数据库,用户可以在其中搜索卡。现在数据库是本地的,带有json文件。问题是,我有一个输入,用户在其中写入卡的“种族”,它用于过滤共享该种族的卡的图像网格 卡片类别如下: export class Card { name: string; attribute: string; type: string; racesOrTraits: string[]; text: string; set: string; code
export class Card {
name: string;
attribute: string;
type: string;
racesOrTraits: string[];
text: string;
set: string;
code: string;
rarity: string;
thumbnailImage: string;
cardImage: string;
}
我有两个页面组件,一个是要过滤的卡片列表,另一个是带有sidenav的容器,输入在其中
网格模板是:
<div class="card-grid">
<md-grid-list cols="4" rowHeight="500px" gutterSize="1em">
<md-grid-tile *ngFor="let card of cards | searchFilter:searchedName | raceFilter:searchedRace " md-cols="4">
<img src="{{card.thumbnailImage}}">
</md-grid-tile>
</md-grid-list>
</div>
排水管为:
@Pipe({
name: 'raceFilter',
pure: false
})
@Injectable()
export class SearchByRacePipe implements PipeTransform {
transform(items: any[], race: any): any {
if (race != undefined) {
// filter items array, items which match and return true will be kept, false will be filtered out
return items.filter(item => item.racesOrTraits.map(function (r: string) {
return r.toLowerCase();
}).includes(race.toLowerCase()));
}
return items;
}
}
希望我能解释清楚。提前感谢您应该从赛车场进行筛选,如下所示
transform(items: any[], race: any): any {
let filteredItems : any[] = new Array();
if (race != undefined) {
// filter items array, items which match and return true will be kept, false will be filtered out
items.forEach((card)={
card.forEach((item)=> {
let temp= item.racesOrTraits.toLowerCase().includes(race.toLowerCase());
if(temp){
filteredItems.push(card);
}
})
})
return filteredItems;
}
您应该从赛车场训练中筛选它,如下所示
transform(items: any[], race: any): any {
let filteredItems : any[] = new Array();
if (race != undefined) {
// filter items array, items which match and return true will be kept, false will be filtered out
items.forEach((card)={
card.forEach((item)=> {
let temp= item.racesOrTraits.toLowerCase().includes(race.toLowerCase());
if(temp){
filteredItems.push(card);
}
})
})
return filteredItems;
}
据我所知,array.includes查找的是确切的项,而不是子集。顺便说一句,此方法不支持IE。请尝试改用indexOf。据我所知,array.includes查找的是确切的项,而不是子集。顺便说一句,此方法不支持IE。请尝试改用indexOf。谢谢!这帮了大忙:谢谢!这帮了大忙:D
transform(items: any[], race: any): any {
let filteredItems : any[] = new Array();
if (race != undefined) {
// filter items array, items which match and return true will be kept, false will be filtered out
items.forEach((card)={
card.forEach((item)=> {
let temp= item.racesOrTraits.toLowerCase().includes(race.toLowerCase());
if(temp){
filteredItems.push(card);
}
})
})
return filteredItems;
}