我在 if else 条件中重复使用相同的数组过滤器功能,但每种情况下只有属性不同。有没有办法将它们组合在一起,还是像下面这样正确?private _filter(value: string, filterIndex, type: string): string[] { let filterArray = []; const filterValue = value.toLowerCase(); if (filterIndex == 0) { // Index for Type if (type === 'Dsc') { this.assetTypeData.filter((option) => option.assetTypeDsc.toLowerCase().includes(filterValue)).forEach(element => { filterArray.push(element.assetTypeDsc) }); } else { this.assetTypeData.filter((option) => option.assetTypeCde.toLowerCase().includes(filterValue)).forEach(element => { filterArray.push(element.assetTypeCde) }); } } else if (filterIndex == 1) { // Index for Make if (type === 'Dsc') { this.assetMakeData.filter((option) => option.assetMakeDsc.toLowerCase().includes(filterValue)).forEach(element => { filterArray.push(element.assetMakeDsc) }); } else { this.assetMakeData.filter((option) => option.assetMakeCde.toLowerCase().includes(filterValue)).forEach(element => { filterArray.push(element.assetMakeCde) }); }}
1 回答
千巷猫影
TA贡献1829条经验 获得超7个赞
试图将所有可以分组的东西分组
private _filter(value: string, filterIndex, type: string): string[] {
const filterValue = value.toLowerCase();
return (filterIndex == 0 ? this.assetTypeData : this.assetMakeData)
.map(option => option[(filterIndex == 0 ? 'assetType': 'assetMake') + type])
.filter(assetValue => assetValue.toLowerCase().includes(filterValue));
}
添加回答
举报
0/150
提交
取消