Angular 6 multi field filter









up vote
0
down vote

favorite












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?










share|improve this question

























    up vote
    0
    down vote

    favorite












    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?










    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      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?










      share|improve this question













      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?







      javascript angular frontend filtering






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 15:03









      Patrick Star

      193




      193






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          This is what you could do to create a multifilter without many if statements.



          Step one:
          Create an object that performs various comparison functions:



          let compareFunctions = 
          equal: function(a,b)
          return a === b;
          ,
          in: function(a,b)
          return a.indexOf(b) !== -1




          Step two:
          Create a function that has the following parameters:



          1. key - The key of the field of your record you want to filter.

          2. value - The value you want to filter by

          3. compareFn - The comparison function to be used for this field

          This function returns a function that executes the condition.



          function condition(key, value, comparFn = compareFunctions.equal) 
          return function(data)
          return comparFn(data[key],value);




          Step three:
          Create an array with "condition" functions representing your filter values:



          let filterArray = [
          condition('category', 'sports'),
          condition('title', 'goal', compareFunctions.in),
          condition('author', 'john'),
          condition('tags', 'hokey', compareFunctions.in),
          ]


          Step four:
          Filter your record by calling your array of conditions functions for each entry and evaluating the result of each condition:



          let result = dataset.filter(y => 
          let resolved = filterArray.map(x => x(y))
          return resolved.every(x => x === true);
          )


          Full example code:



          let compareFunctions = 
          equal: function(a,b)
          return a === b;
          ,
          in: function(a,b)
          return a.indexOf(b) !== -1



          function condition(key, value, comparFn = compareFunctions.equal)
          return function(data)
          return comparFn(data[key],value);



          let dataset = [

          category: "sports",
          title: "goal goal goal",
          author: "john",
          tags: ["hokey", "ice-hokey"]
          ,

          category: "news",
          title: "bla bla",
          author: "Timo",
          tags: ["news"]
          ,

          category: "news",
          title: "blub blub",
          author: "alex",
          tags: ["hokey", "ice-hokey"]
          ,

          category: "sports",
          title: "Kölner Haie bla bla",
          author: "Timo",
          tags: ["hokey", "ice-hokey"]

          ]

          let filterArray = [
          condition('category', 'sports'),
          condition('title', 'goal', compareFunctions.in),
          condition('author', 'john'),
          condition('tags', 'hokey', compareFunctions.in),
          ]

          //console.log(filterArray)

          let result = dataset.filter(y =>
          let resolved = filterArray.map(x => x(y))
          return resolved.every(x => x === true);
          )

          console.log(result)





          share|improve this answer






















          • thank you. This is something I have been looking for.
            – Patrick Star
            Nov 12 at 7:37










          • Im glad to help.
            – mr.void
            Nov 12 at 13:05










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250009%2fangular-6-multi-field-filter%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote













          This is what you could do to create a multifilter without many if statements.



          Step one:
          Create an object that performs various comparison functions:



          let compareFunctions = 
          equal: function(a,b)
          return a === b;
          ,
          in: function(a,b)
          return a.indexOf(b) !== -1




          Step two:
          Create a function that has the following parameters:



          1. key - The key of the field of your record you want to filter.

          2. value - The value you want to filter by

          3. compareFn - The comparison function to be used for this field

          This function returns a function that executes the condition.



          function condition(key, value, comparFn = compareFunctions.equal) 
          return function(data)
          return comparFn(data[key],value);




          Step three:
          Create an array with "condition" functions representing your filter values:



          let filterArray = [
          condition('category', 'sports'),
          condition('title', 'goal', compareFunctions.in),
          condition('author', 'john'),
          condition('tags', 'hokey', compareFunctions.in),
          ]


          Step four:
          Filter your record by calling your array of conditions functions for each entry and evaluating the result of each condition:



          let result = dataset.filter(y => 
          let resolved = filterArray.map(x => x(y))
          return resolved.every(x => x === true);
          )


          Full example code:



          let compareFunctions = 
          equal: function(a,b)
          return a === b;
          ,
          in: function(a,b)
          return a.indexOf(b) !== -1



          function condition(key, value, comparFn = compareFunctions.equal)
          return function(data)
          return comparFn(data[key],value);



          let dataset = [

          category: "sports",
          title: "goal goal goal",
          author: "john",
          tags: ["hokey", "ice-hokey"]
          ,

          category: "news",
          title: "bla bla",
          author: "Timo",
          tags: ["news"]
          ,

          category: "news",
          title: "blub blub",
          author: "alex",
          tags: ["hokey", "ice-hokey"]
          ,

          category: "sports",
          title: "Kölner Haie bla bla",
          author: "Timo",
          tags: ["hokey", "ice-hokey"]

          ]

          let filterArray = [
          condition('category', 'sports'),
          condition('title', 'goal', compareFunctions.in),
          condition('author', 'john'),
          condition('tags', 'hokey', compareFunctions.in),
          ]

          //console.log(filterArray)

          let result = dataset.filter(y =>
          let resolved = filterArray.map(x => x(y))
          return resolved.every(x => x === true);
          )

          console.log(result)





          share|improve this answer






















          • thank you. This is something I have been looking for.
            – Patrick Star
            Nov 12 at 7:37










          • Im glad to help.
            – mr.void
            Nov 12 at 13:05














          up vote
          0
          down vote













          This is what you could do to create a multifilter without many if statements.



          Step one:
          Create an object that performs various comparison functions:



          let compareFunctions = 
          equal: function(a,b)
          return a === b;
          ,
          in: function(a,b)
          return a.indexOf(b) !== -1




          Step two:
          Create a function that has the following parameters:



          1. key - The key of the field of your record you want to filter.

          2. value - The value you want to filter by

          3. compareFn - The comparison function to be used for this field

          This function returns a function that executes the condition.



          function condition(key, value, comparFn = compareFunctions.equal) 
          return function(data)
          return comparFn(data[key],value);




          Step three:
          Create an array with "condition" functions representing your filter values:



          let filterArray = [
          condition('category', 'sports'),
          condition('title', 'goal', compareFunctions.in),
          condition('author', 'john'),
          condition('tags', 'hokey', compareFunctions.in),
          ]


          Step four:
          Filter your record by calling your array of conditions functions for each entry and evaluating the result of each condition:



          let result = dataset.filter(y => 
          let resolved = filterArray.map(x => x(y))
          return resolved.every(x => x === true);
          )


          Full example code:



          let compareFunctions = 
          equal: function(a,b)
          return a === b;
          ,
          in: function(a,b)
          return a.indexOf(b) !== -1



          function condition(key, value, comparFn = compareFunctions.equal)
          return function(data)
          return comparFn(data[key],value);



          let dataset = [

          category: "sports",
          title: "goal goal goal",
          author: "john",
          tags: ["hokey", "ice-hokey"]
          ,

          category: "news",
          title: "bla bla",
          author: "Timo",
          tags: ["news"]
          ,

          category: "news",
          title: "blub blub",
          author: "alex",
          tags: ["hokey", "ice-hokey"]
          ,

          category: "sports",
          title: "Kölner Haie bla bla",
          author: "Timo",
          tags: ["hokey", "ice-hokey"]

          ]

          let filterArray = [
          condition('category', 'sports'),
          condition('title', 'goal', compareFunctions.in),
          condition('author', 'john'),
          condition('tags', 'hokey', compareFunctions.in),
          ]

          //console.log(filterArray)

          let result = dataset.filter(y =>
          let resolved = filterArray.map(x => x(y))
          return resolved.every(x => x === true);
          )

          console.log(result)





          share|improve this answer






















          • thank you. This is something I have been looking for.
            – Patrick Star
            Nov 12 at 7:37










          • Im glad to help.
            – mr.void
            Nov 12 at 13:05












          up vote
          0
          down vote










          up vote
          0
          down vote









          This is what you could do to create a multifilter without many if statements.



          Step one:
          Create an object that performs various comparison functions:



          let compareFunctions = 
          equal: function(a,b)
          return a === b;
          ,
          in: function(a,b)
          return a.indexOf(b) !== -1




          Step two:
          Create a function that has the following parameters:



          1. key - The key of the field of your record you want to filter.

          2. value - The value you want to filter by

          3. compareFn - The comparison function to be used for this field

          This function returns a function that executes the condition.



          function condition(key, value, comparFn = compareFunctions.equal) 
          return function(data)
          return comparFn(data[key],value);




          Step three:
          Create an array with "condition" functions representing your filter values:



          let filterArray = [
          condition('category', 'sports'),
          condition('title', 'goal', compareFunctions.in),
          condition('author', 'john'),
          condition('tags', 'hokey', compareFunctions.in),
          ]


          Step four:
          Filter your record by calling your array of conditions functions for each entry and evaluating the result of each condition:



          let result = dataset.filter(y => 
          let resolved = filterArray.map(x => x(y))
          return resolved.every(x => x === true);
          )


          Full example code:



          let compareFunctions = 
          equal: function(a,b)
          return a === b;
          ,
          in: function(a,b)
          return a.indexOf(b) !== -1



          function condition(key, value, comparFn = compareFunctions.equal)
          return function(data)
          return comparFn(data[key],value);



          let dataset = [

          category: "sports",
          title: "goal goal goal",
          author: "john",
          tags: ["hokey", "ice-hokey"]
          ,

          category: "news",
          title: "bla bla",
          author: "Timo",
          tags: ["news"]
          ,

          category: "news",
          title: "blub blub",
          author: "alex",
          tags: ["hokey", "ice-hokey"]
          ,

          category: "sports",
          title: "Kölner Haie bla bla",
          author: "Timo",
          tags: ["hokey", "ice-hokey"]

          ]

          let filterArray = [
          condition('category', 'sports'),
          condition('title', 'goal', compareFunctions.in),
          condition('author', 'john'),
          condition('tags', 'hokey', compareFunctions.in),
          ]

          //console.log(filterArray)

          let result = dataset.filter(y =>
          let resolved = filterArray.map(x => x(y))
          return resolved.every(x => x === true);
          )

          console.log(result)





          share|improve this answer














          This is what you could do to create a multifilter without many if statements.



          Step one:
          Create an object that performs various comparison functions:



          let compareFunctions = 
          equal: function(a,b)
          return a === b;
          ,
          in: function(a,b)
          return a.indexOf(b) !== -1




          Step two:
          Create a function that has the following parameters:



          1. key - The key of the field of your record you want to filter.

          2. value - The value you want to filter by

          3. compareFn - The comparison function to be used for this field

          This function returns a function that executes the condition.



          function condition(key, value, comparFn = compareFunctions.equal) 
          return function(data)
          return comparFn(data[key],value);




          Step three:
          Create an array with "condition" functions representing your filter values:



          let filterArray = [
          condition('category', 'sports'),
          condition('title', 'goal', compareFunctions.in),
          condition('author', 'john'),
          condition('tags', 'hokey', compareFunctions.in),
          ]


          Step four:
          Filter your record by calling your array of conditions functions for each entry and evaluating the result of each condition:



          let result = dataset.filter(y => 
          let resolved = filterArray.map(x => x(y))
          return resolved.every(x => x === true);
          )


          Full example code:



          let compareFunctions = 
          equal: function(a,b)
          return a === b;
          ,
          in: function(a,b)
          return a.indexOf(b) !== -1



          function condition(key, value, comparFn = compareFunctions.equal)
          return function(data)
          return comparFn(data[key],value);



          let dataset = [

          category: "sports",
          title: "goal goal goal",
          author: "john",
          tags: ["hokey", "ice-hokey"]
          ,

          category: "news",
          title: "bla bla",
          author: "Timo",
          tags: ["news"]
          ,

          category: "news",
          title: "blub blub",
          author: "alex",
          tags: ["hokey", "ice-hokey"]
          ,

          category: "sports",
          title: "Kölner Haie bla bla",
          author: "Timo",
          tags: ["hokey", "ice-hokey"]

          ]

          let filterArray = [
          condition('category', 'sports'),
          condition('title', 'goal', compareFunctions.in),
          condition('author', 'john'),
          condition('tags', 'hokey', compareFunctions.in),
          ]

          //console.log(filterArray)

          let result = dataset.filter(y =>
          let resolved = filterArray.map(x => x(y))
          return resolved.every(x => x === true);
          )

          console.log(result)






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 11 at 16:07

























          answered Nov 11 at 16:01









          mr.void

          1,5252623




          1,5252623











          • thank you. This is something I have been looking for.
            – Patrick Star
            Nov 12 at 7:37










          • Im glad to help.
            – mr.void
            Nov 12 at 13:05
















          • thank you. This is something I have been looking for.
            – Patrick Star
            Nov 12 at 7:37










          • Im glad to help.
            – mr.void
            Nov 12 at 13:05















          thank you. This is something I have been looking for.
          – Patrick Star
          Nov 12 at 7:37




          thank you. This is something I have been looking for.
          – Patrick Star
          Nov 12 at 7:37












          Im glad to help.
          – mr.void
          Nov 12 at 13:05




          Im glad to help.
          – mr.void
          Nov 12 at 13:05

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250009%2fangular-6-multi-field-filter%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Barbados

          How to read a connectionString WITH PROVIDER in .NET Core?

          Node.js Script on GitHub Pages or Amazon S3