我有一个我不满意的角度过滤器。
过滤器遍布一系列公司,每家公司都有一个拥有多名员工的员工阵列。
这个过滤器工作正常,但对我来说,它似乎很差,并且只是简单的丑陋
有没有更有效的方法来编写这个过滤器?
过滤器代码:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'filter',
pure: false
})
export class SpFilterPipe implements PipeTransform {
transform(value: any, args?: any): any {
if (value.length === 0) {
return value;
}
let resultArray = [];
for (let item of value) {
if (
~item.name.toLowerCase().indexOf(args) ||
~item.employees[0].displayname.toLowerCase().indexOf(args) ||
~item.employees[0].fullname.toLowerCase().indexOf(args)
) {
resultArray.push(item);
}
if (args.length > 2) {
for (let employee of item.employees) {
if(
~employee.displayname.toLowerCase().indexOf(args) ||
~employee.fullname.toLowerCase().indexOf(args)
) {
if (item.employees.indexOf(employee)){
resultArray.push(item);
}
}
}
}
}
return resultArray;
}
}
我正在循环的JSON对象:
{
"companies" : [ {
"employees" : [ {
"displayname" : "F.H.Willemsen",
"fullname" : "Fred Harold Willemsen",
"number" : "001"
}, {
"displayname" : "D.Muys",
"fullname" : "Dennis Muys",
"number" : "002"
} ],
"floor" : 21,
"name" : "nothing",
"room" : "21.21 - 31.31"
}, {
"employees" : [ {
"displayname" : "A.Testingperson",
"fullname" : "Adriaan Testingperson",
"number" : "003"
}, {
"displayname" : "B.Muys",
"fullname" : "Bassie Muys",
"number" : "004"
} ],
"floor" : 20,
"name" : "TestingCompany",
"room" : "20.20 - 30.30"
}, {
"employees" : [ {
"displayname" : "D.Kort",
"fullname" : "Dirk Kort",
"number" : "005"
}, {
"displayname" : "K.Lang",
"fullname" : "Karel Lang",
"number" : "006"
} ],
"floor" : 22,
"name" : "Bla",
"room" : "22.22 - 32.32"
}, {
"employees" : [ {
"displayname" : "A.Panamera",
"fullname" : "Abdullah Panamera",
"number" : "007"
}, {
"displayname" : "D.Nguyen",
"fullname" : "Dong Nguyen",
"number" : "008"
} ],
"floor" : 23,
"name" : "Apple Dell",
"room" : "23.23 - 33.33"
}, {
"employees" : [ {
"displayname" : "B.Kadjar",
"fullname" : "Barry Kadjar",
"number" : "009"
}, {
"displayname" : "G.Option",
"fullname" : "Gerard Option",
"number" : "010"
} ],
"floor" : 24,
"name" : "Magic Trackpad",
"room" : "24.24 - 34.34"
} ]
}