我在 if else 條件中重復使用相同的數(shù)組過濾器功能,但每種情況下只有屬性不同。有沒有辦法將它們組合在一起,還是像下面這樣正確?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) }); }}
將下面代碼中相似的代碼行分組在一起以進行一些數(shù)組過濾
繁花不似錦
2023-11-12 15:03:32