I find myself using the same array filter function repeatedly within my if else conditions, with only the properties changing in each case. Is there a way to consolidate them or is it fine as it is currently structured below?
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)
});
}