I'm using javascript to search through a google spreadsheet for items. I have several available filters, the issue is that for each filter I add in, I seem to have exponentially more if/else statements.
Let's say I can filter by name, type, and date. Date is required, but the other two options are not. I end up having to have an if/else for each unique combination to make sure I'm matching the correct inputs. If I want to add more ways to filter data it will get even messier.
Example:
var filterReportType = !CheckIfBlank(reportType);
var filterName = !CheckIfBlank(name);
for(var i = 2; i < dataRange.length; i++)
{
if(dataRange[i][0] != "")
{
searchData.entriesSearched ++;
if(CompareEntryDates(new Date(startDate), new Date(endDate), new Date(dataRange[i][0])))
{
if(filterName && filterReportType)
{
if(name == dataRange[i][2] && reportType == dataRange[i][3])
{
var newEntry = FillEntryObject(i, dataRange);
entries.push(newEntry);
searchData.entriesFound ++;
}
}
else if(filterName)
{
if(name == dataRange[i][2])
{
var newEntry = FillEntryObject(i, dataRange);
entries.push(newEntry);
searchData.entriesFound ++;
}
}
else if(filterReportType)
{
if(reportType == dataRange[i][3])
{
var newEntry = FillEntryObject(i, dataRange);
entries.push(newEntry);
searchData.entriesFound ++;
}
}
else
{
var newEntry = FillEntryObject(i, dataRange);
entries.push(newEntry);
searchData.entriesFound ++;
}
}
}
}
This is not a maintainable pattern, if I need to add more possible inputs to filter by then this chain of statements will become massive.
Edit: Why this is not a duplicate: I checked out that other question, and I see how it will shorten a very long statement, but I do not see how it will shorten a long chain of separate statements. I would still need to check for different combinations and I believe I would end up wit the same number of if/else statements anyways.
Aucun commentaire:
Enregistrer un commentaire