过滤管中较短的情况
Shorter condition in the filter pipe
我有一个管道,可以过滤客户的日期。现在看起来像:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'filter'
})
export class FilterPipe implements PipeTransform {
transform(clients: any[], searchText: string): any[] {
if(!clients) return [];
if(!searchText) return clients;
searchText = searchText.toLowerCase();
return clients.filter( it => {
if (it.firstName.toLowerCase().includes(searchText)) {
return it.firstName.toLowerCase().includes(searchText);
}
if (it.lastName.toLowerCase().includes(searchText)) {
return it.lastName.toLowerCase().includes(searchText);
}
if (it.street.toLowerCase().includes(searchText)) {
return it.street.toLowerCase().includes(searchText);
}
if (it.city.toLowerCase().includes(searchText)) {
return it.city.toLowerCase().includes(searchText);
}
if (it.zipCode.toLowerCase().includes(searchText)) {
return it.zipCode.toLowerCase().includes(searchText);
}
if (it.country.toLowerCase().includes(searchText)) {
return it.country.toLowerCase().includes(searchText);
}
if (it.email.toLowerCase().includes(searchText)) {
return it.email.toLowerCase().includes(searchText);
}
if (it.phone.toLowerCase().includes(searchText)) {
return it.phone.toLowerCase().includes(searchText);
}
});
}
}
但是我明白,条件可以写得更短。请告诉我,我该怎么做?..
这里是一个更简短的写条件的方法:
return clients.filter(it =>
it.firstName.toLowerCase().includes(searchText) ||
it.lastName.toLowerCase().includes(searchText) ||
it.street.toLowerCase().includes(searchText) ||
it.city.toLowerCase().includes(searchText) ||
it.zipCode.toLowerCase().includes(searchText) ||
it.country.toLowerCase().includes(searchText) ||
it.email.toLowerCase().includes(searchText) ||
it.phone.toLowerCase().includes(searchText)
);
你可以这样使用
return clients.filter(function (item: any) {
for (let property in item) {
if (item[property] === null) {
continue;
}
if (item[property].toString().toLowerCase().includes(searchText)) {
return true;
}
}
return false;
});
我有一个管道,可以过滤客户的日期。现在看起来像:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'filter'
})
export class FilterPipe implements PipeTransform {
transform(clients: any[], searchText: string): any[] {
if(!clients) return [];
if(!searchText) return clients;
searchText = searchText.toLowerCase();
return clients.filter( it => {
if (it.firstName.toLowerCase().includes(searchText)) {
return it.firstName.toLowerCase().includes(searchText);
}
if (it.lastName.toLowerCase().includes(searchText)) {
return it.lastName.toLowerCase().includes(searchText);
}
if (it.street.toLowerCase().includes(searchText)) {
return it.street.toLowerCase().includes(searchText);
}
if (it.city.toLowerCase().includes(searchText)) {
return it.city.toLowerCase().includes(searchText);
}
if (it.zipCode.toLowerCase().includes(searchText)) {
return it.zipCode.toLowerCase().includes(searchText);
}
if (it.country.toLowerCase().includes(searchText)) {
return it.country.toLowerCase().includes(searchText);
}
if (it.email.toLowerCase().includes(searchText)) {
return it.email.toLowerCase().includes(searchText);
}
if (it.phone.toLowerCase().includes(searchText)) {
return it.phone.toLowerCase().includes(searchText);
}
});
}
}
但是我明白,条件可以写得更短。请告诉我,我该怎么做?..
这里是一个更简短的写条件的方法:
return clients.filter(it =>
it.firstName.toLowerCase().includes(searchText) ||
it.lastName.toLowerCase().includes(searchText) ||
it.street.toLowerCase().includes(searchText) ||
it.city.toLowerCase().includes(searchText) ||
it.zipCode.toLowerCase().includes(searchText) ||
it.country.toLowerCase().includes(searchText) ||
it.email.toLowerCase().includes(searchText) ||
it.phone.toLowerCase().includes(searchText)
);
你可以这样使用
return clients.filter(function (item: any) {
for (let property in item) {
if (item[property] === null) {
continue;
}
if (item[property].toString().toLowerCase().includes(searchText)) {
return true;
}
}
return false;
});