I'm currently working on angular 6 app. I have a huge list of articles and want to do some filtering upon it. Right now it works real-time, there is no button to submit your filter options, it all happens as you type. I figure a way, but it has still some issues I can fix, but doesn't like the way I did it. I'm sure, there has to be something more elegant.
For better imagination those articles have category, title, author, tags. I am able to filter them according to category lets say... but I want to do some kind of filtering.
Example: Filter all articles from category 'sports', then filter all articles having substring 'goal' in title from that already filtered array, then filter those whos author is 'john' and then filter all with tag 'hockey'.
I ended up with huge amount of IF statements, which is not the right approach I would say. Can you please recommend me some better way to do this?
This is what you could do to create a multifilter without many if statements.
Step one: Create an object that performs various comparison functions:
Step two: Create a function that has the following parameters:
This function returns a function that executes the condition.
Step three: Create an array with "condition" functions representing your filter values:
Step four: Filter your record by calling your array of conditions functions for each entry and evaluating the result of each condition:
Full example code: