Filter list based on more than one field











up vote
1
down vote

favorite












I'm iterating a list of jobs and there's a search implemented on this list.
Search is working but now it only filters list based on one field.



Here's my list:



<ion-card *ngFor="let job of allJobs | search : searchTerm">
<ion-grid>
<ion-row>
<ion-col>
<div>
<span> {{job.day | uppercase}}</span>
<span> {{job.month | uppercase}}</span>
</div>
</ion-col>

<ion-col>
<div>
<span>{{job.time}}</span>
<span>{{job.name}}</span>
</div>
</ion-col>
</ion-row>
</ion-grid>
</ion-card>


I made a pipe for implementing search. Here's the code for it.



  transform(items: any, terms: string): any {
if(!items) return ;
if(!terms) return items;
terms = terms.toLowerCase();
return items.filter( it => {
return it.name.toLowerCase().includes(terms); // only filter name
});
}


Now the list gets filtered only based on the name field. I wanna filter the list based on day, month and time as well.



Can anyone tell me how to make this happen?



Sample Data for Jobs. Jobs is an array of objects



   [  
{
"id":10,
"day":"Monday",
"month":"June",
"time":"10",
"name":"John",
"email":"john@gmail.com"
},
{
"id":11,
"day":"Tuesday",
"month":"May",
"time":"12",
"name":"Jane",
"email":"jane@gmail.com"
},
{
"id":12,
"day":"Friday",
"month":"June",
"time":"16",
"name":"",
"email":"john@gmail.com"
},
{
"id":13,
"day":"Tuesday",
"month":"August",
"time":"21",
"name":"",
"email":"kevin@gmail.com"
},
{
"id":14,
"day":"Saturday",
"month":"December",
"time":"12",
"name":"Sam",
"email":"sam@gmail.com"
},

]


And searchTerm is just a string.



As you can see, there are more fields in the sample data than the one displayed in the HTML but I'm trying only to search for the fields that are displayed in the HTML. Some fields can have null values (for eg. name in the sample data has two null values)



I tried the solutions already provided but none of them are working for my requirement.



P.S: Read somewhere that pipes are not the best option to do functionality like this. I'm ready to implement this logic in the class as well.










share|improve this question

















This question has an open bounty worth +50
reputation from Annabelle ending in 6 days.


The question is widely applicable to a large audience. A detailed canonical answer is required to address all the concerns.




















    up vote
    1
    down vote

    favorite












    I'm iterating a list of jobs and there's a search implemented on this list.
    Search is working but now it only filters list based on one field.



    Here's my list:



    <ion-card *ngFor="let job of allJobs | search : searchTerm">
    <ion-grid>
    <ion-row>
    <ion-col>
    <div>
    <span> {{job.day | uppercase}}</span>
    <span> {{job.month | uppercase}}</span>
    </div>
    </ion-col>

    <ion-col>
    <div>
    <span>{{job.time}}</span>
    <span>{{job.name}}</span>
    </div>
    </ion-col>
    </ion-row>
    </ion-grid>
    </ion-card>


    I made a pipe for implementing search. Here's the code for it.



      transform(items: any, terms: string): any {
    if(!items) return ;
    if(!terms) return items;
    terms = terms.toLowerCase();
    return items.filter( it => {
    return it.name.toLowerCase().includes(terms); // only filter name
    });
    }


    Now the list gets filtered only based on the name field. I wanna filter the list based on day, month and time as well.



    Can anyone tell me how to make this happen?



    Sample Data for Jobs. Jobs is an array of objects



       [  
    {
    "id":10,
    "day":"Monday",
    "month":"June",
    "time":"10",
    "name":"John",
    "email":"john@gmail.com"
    },
    {
    "id":11,
    "day":"Tuesday",
    "month":"May",
    "time":"12",
    "name":"Jane",
    "email":"jane@gmail.com"
    },
    {
    "id":12,
    "day":"Friday",
    "month":"June",
    "time":"16",
    "name":"",
    "email":"john@gmail.com"
    },
    {
    "id":13,
    "day":"Tuesday",
    "month":"August",
    "time":"21",
    "name":"",
    "email":"kevin@gmail.com"
    },
    {
    "id":14,
    "day":"Saturday",
    "month":"December",
    "time":"12",
    "name":"Sam",
    "email":"sam@gmail.com"
    },

    ]


    And searchTerm is just a string.



    As you can see, there are more fields in the sample data than the one displayed in the HTML but I'm trying only to search for the fields that are displayed in the HTML. Some fields can have null values (for eg. name in the sample data has two null values)



    I tried the solutions already provided but none of them are working for my requirement.



    P.S: Read somewhere that pipes are not the best option to do functionality like this. I'm ready to implement this logic in the class as well.










    share|improve this question

















    This question has an open bounty worth +50
    reputation from Annabelle ending in 6 days.


    The question is widely applicable to a large audience. A detailed canonical answer is required to address all the concerns.


















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I'm iterating a list of jobs and there's a search implemented on this list.
      Search is working but now it only filters list based on one field.



      Here's my list:



      <ion-card *ngFor="let job of allJobs | search : searchTerm">
      <ion-grid>
      <ion-row>
      <ion-col>
      <div>
      <span> {{job.day | uppercase}}</span>
      <span> {{job.month | uppercase}}</span>
      </div>
      </ion-col>

      <ion-col>
      <div>
      <span>{{job.time}}</span>
      <span>{{job.name}}</span>
      </div>
      </ion-col>
      </ion-row>
      </ion-grid>
      </ion-card>


      I made a pipe for implementing search. Here's the code for it.



        transform(items: any, terms: string): any {
      if(!items) return ;
      if(!terms) return items;
      terms = terms.toLowerCase();
      return items.filter( it => {
      return it.name.toLowerCase().includes(terms); // only filter name
      });
      }


      Now the list gets filtered only based on the name field. I wanna filter the list based on day, month and time as well.



      Can anyone tell me how to make this happen?



      Sample Data for Jobs. Jobs is an array of objects



         [  
      {
      "id":10,
      "day":"Monday",
      "month":"June",
      "time":"10",
      "name":"John",
      "email":"john@gmail.com"
      },
      {
      "id":11,
      "day":"Tuesday",
      "month":"May",
      "time":"12",
      "name":"Jane",
      "email":"jane@gmail.com"
      },
      {
      "id":12,
      "day":"Friday",
      "month":"June",
      "time":"16",
      "name":"",
      "email":"john@gmail.com"
      },
      {
      "id":13,
      "day":"Tuesday",
      "month":"August",
      "time":"21",
      "name":"",
      "email":"kevin@gmail.com"
      },
      {
      "id":14,
      "day":"Saturday",
      "month":"December",
      "time":"12",
      "name":"Sam",
      "email":"sam@gmail.com"
      },

      ]


      And searchTerm is just a string.



      As you can see, there are more fields in the sample data than the one displayed in the HTML but I'm trying only to search for the fields that are displayed in the HTML. Some fields can have null values (for eg. name in the sample data has two null values)



      I tried the solutions already provided but none of them are working for my requirement.



      P.S: Read somewhere that pipes are not the best option to do functionality like this. I'm ready to implement this logic in the class as well.










      share|improve this question















      I'm iterating a list of jobs and there's a search implemented on this list.
      Search is working but now it only filters list based on one field.



      Here's my list:



      <ion-card *ngFor="let job of allJobs | search : searchTerm">
      <ion-grid>
      <ion-row>
      <ion-col>
      <div>
      <span> {{job.day | uppercase}}</span>
      <span> {{job.month | uppercase}}</span>
      </div>
      </ion-col>

      <ion-col>
      <div>
      <span>{{job.time}}</span>
      <span>{{job.name}}</span>
      </div>
      </ion-col>
      </ion-row>
      </ion-grid>
      </ion-card>


      I made a pipe for implementing search. Here's the code for it.



        transform(items: any, terms: string): any {
      if(!items) return ;
      if(!terms) return items;
      terms = terms.toLowerCase();
      return items.filter( it => {
      return it.name.toLowerCase().includes(terms); // only filter name
      });
      }


      Now the list gets filtered only based on the name field. I wanna filter the list based on day, month and time as well.



      Can anyone tell me how to make this happen?



      Sample Data for Jobs. Jobs is an array of objects



         [  
      {
      "id":10,
      "day":"Monday",
      "month":"June",
      "time":"10",
      "name":"John",
      "email":"john@gmail.com"
      },
      {
      "id":11,
      "day":"Tuesday",
      "month":"May",
      "time":"12",
      "name":"Jane",
      "email":"jane@gmail.com"
      },
      {
      "id":12,
      "day":"Friday",
      "month":"June",
      "time":"16",
      "name":"",
      "email":"john@gmail.com"
      },
      {
      "id":13,
      "day":"Tuesday",
      "month":"August",
      "time":"21",
      "name":"",
      "email":"kevin@gmail.com"
      },
      {
      "id":14,
      "day":"Saturday",
      "month":"December",
      "time":"12",
      "name":"Sam",
      "email":"sam@gmail.com"
      },

      ]


      And searchTerm is just a string.



      As you can see, there are more fields in the sample data than the one displayed in the HTML but I'm trying only to search for the fields that are displayed in the HTML. Some fields can have null values (for eg. name in the sample data has two null values)



      I tried the solutions already provided but none of them are working for my requirement.



      P.S: Read somewhere that pipes are not the best option to do functionality like this. I'm ready to implement this logic in the class as well.







      ionic-framework ionic2 ionic3






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited yesterday

























      asked Nov 7 at 7:29









      Annabelle

      504823




      504823






      This question has an open bounty worth +50
      reputation from Annabelle ending in 6 days.


      The question is widely applicable to a large audience. A detailed canonical answer is required to address all the concerns.








      This question has an open bounty worth +50
      reputation from Annabelle ending in 6 days.


      The question is widely applicable to a large audience. A detailed canonical answer is required to address all the concerns.


























          3 Answers
          3






          active

          oldest

          votes

















          up vote
          1
          down vote













          Try this code.. it's pretty simple.



            transform(items: any, terms: string): any {
          if (!items) return ;
          if (!terms) return items;
          terms = terms.toLowerCase();
          terms = terms.trim();
          return items.filter(it => {
          if (it.day) {
          return it.day.toLowerCase().includes(terms);
          }
          if (it.month) {
          return it.month.toLowerCase().includes(terms);
          }
          if (it.time) {
          return it.time.toLowerCase().includes(terms);
          }
          if (it.name) {
          return it.name.toLowerCase().includes(terms);
          }
          });
          }





          share|improve this answer





















          • This code is not working. I have sample data and updated the question.
            – Annabelle
            5 hours ago


















          up vote
          0
          down vote













          This dont work because the includes dont test for multiple term cases. You didnt say whats inside the items Array but if it is a String you could do this:



          transform(items: any, terms: string): any {
          if(!items) return ;
          if(!terms) return items;
          terms = terms.toLowerCase();
          return items.filter( it => {
          //if it is something like "Programmer 07 November 12:00PM"
          var informations = it.split(' '); //["Programmer", "07" ,"November" ,"12:00PM"]
          var termArray = terms.split(' ');
          var rightResult = true;
          for (var index in termArray) {
          if !(informations.include(termArray[index])) {
          rightResult = false;
          }
          return rightResult;


          });
          }





          share|improve this answer








          New contributor




          Jonathan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.


















          • Tried your code. It's not working. I have updated the code with sample data
            – Annabelle
            5 hours ago


















          up vote
          0
          down vote













          Inside your transform method of your search pipe, apply filters on all the fields you want to apply filter on. Following will search for all keys in the object:



          transform(items: any, terms: string): any {
          if(!items) return ;
          if(!terms) return items;
          terms = terms.toLowerCase();
          return items.filter( it => {

          return keys(it).reduce((prev, key) => {
          return prev || key.toLowerCase().includes(term);
          }, false);
          });
          }


          If keys or Object.keys are not working, use the following code instead of reduce function:



          ...
          let bInclude = false;
          for(let key in it){
          bInclude = bInclude || key.toLowerCase().includes(term);
          }

          return bInclude;
          ...





          share|improve this answer























          • ERROR ReferenceError: keys is not defined @Shubham Chaudhary
            – Annabelle
            Nov 8 at 4:57










          • Use Object.keys instead. @Annabelle
            – Shubham Chaudhary
            Nov 8 at 17:17










          • Tried that but it's still not working @Shubham Chaudhary
            – Annabelle
            Nov 9 at 6:50












          • Edited the answer, use for..in for getting object keys as array
            – Shubham Chaudhary
            Nov 9 at 19:13










          • Tried your code. Its still not working. I have sample data and updated the question.
            – Annabelle
            5 hours ago











          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%2f53185106%2ffilter-list-based-on-more-than-one-field%23new-answer', 'question_page');
          }
          );

          Post as a guest
































          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote













          Try this code.. it's pretty simple.



            transform(items: any, terms: string): any {
          if (!items) return ;
          if (!terms) return items;
          terms = terms.toLowerCase();
          terms = terms.trim();
          return items.filter(it => {
          if (it.day) {
          return it.day.toLowerCase().includes(terms);
          }
          if (it.month) {
          return it.month.toLowerCase().includes(terms);
          }
          if (it.time) {
          return it.time.toLowerCase().includes(terms);
          }
          if (it.name) {
          return it.name.toLowerCase().includes(terms);
          }
          });
          }





          share|improve this answer





















          • This code is not working. I have sample data and updated the question.
            – Annabelle
            5 hours ago















          up vote
          1
          down vote













          Try this code.. it's pretty simple.



            transform(items: any, terms: string): any {
          if (!items) return ;
          if (!terms) return items;
          terms = terms.toLowerCase();
          terms = terms.trim();
          return items.filter(it => {
          if (it.day) {
          return it.day.toLowerCase().includes(terms);
          }
          if (it.month) {
          return it.month.toLowerCase().includes(terms);
          }
          if (it.time) {
          return it.time.toLowerCase().includes(terms);
          }
          if (it.name) {
          return it.name.toLowerCase().includes(terms);
          }
          });
          }





          share|improve this answer





















          • This code is not working. I have sample data and updated the question.
            – Annabelle
            5 hours ago













          up vote
          1
          down vote










          up vote
          1
          down vote









          Try this code.. it's pretty simple.



            transform(items: any, terms: string): any {
          if (!items) return ;
          if (!terms) return items;
          terms = terms.toLowerCase();
          terms = terms.trim();
          return items.filter(it => {
          if (it.day) {
          return it.day.toLowerCase().includes(terms);
          }
          if (it.month) {
          return it.month.toLowerCase().includes(terms);
          }
          if (it.time) {
          return it.time.toLowerCase().includes(terms);
          }
          if (it.name) {
          return it.name.toLowerCase().includes(terms);
          }
          });
          }





          share|improve this answer












          Try this code.. it's pretty simple.



            transform(items: any, terms: string): any {
          if (!items) return ;
          if (!terms) return items;
          terms = terms.toLowerCase();
          terms = terms.trim();
          return items.filter(it => {
          if (it.day) {
          return it.day.toLowerCase().includes(terms);
          }
          if (it.month) {
          return it.month.toLowerCase().includes(terms);
          }
          if (it.time) {
          return it.time.toLowerCase().includes(terms);
          }
          if (it.name) {
          return it.name.toLowerCase().includes(terms);
          }
          });
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 8 at 10:33









          John Doe

          73321241




          73321241












          • This code is not working. I have sample data and updated the question.
            – Annabelle
            5 hours ago


















          • This code is not working. I have sample data and updated the question.
            – Annabelle
            5 hours ago
















          This code is not working. I have sample data and updated the question.
          – Annabelle
          5 hours ago




          This code is not working. I have sample data and updated the question.
          – Annabelle
          5 hours ago












          up vote
          0
          down vote













          This dont work because the includes dont test for multiple term cases. You didnt say whats inside the items Array but if it is a String you could do this:



          transform(items: any, terms: string): any {
          if(!items) return ;
          if(!terms) return items;
          terms = terms.toLowerCase();
          return items.filter( it => {
          //if it is something like "Programmer 07 November 12:00PM"
          var informations = it.split(' '); //["Programmer", "07" ,"November" ,"12:00PM"]
          var termArray = terms.split(' ');
          var rightResult = true;
          for (var index in termArray) {
          if !(informations.include(termArray[index])) {
          rightResult = false;
          }
          return rightResult;


          });
          }





          share|improve this answer








          New contributor




          Jonathan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.


















          • Tried your code. It's not working. I have updated the code with sample data
            – Annabelle
            5 hours ago















          up vote
          0
          down vote













          This dont work because the includes dont test for multiple term cases. You didnt say whats inside the items Array but if it is a String you could do this:



          transform(items: any, terms: string): any {
          if(!items) return ;
          if(!terms) return items;
          terms = terms.toLowerCase();
          return items.filter( it => {
          //if it is something like "Programmer 07 November 12:00PM"
          var informations = it.split(' '); //["Programmer", "07" ,"November" ,"12:00PM"]
          var termArray = terms.split(' ');
          var rightResult = true;
          for (var index in termArray) {
          if !(informations.include(termArray[index])) {
          rightResult = false;
          }
          return rightResult;


          });
          }





          share|improve this answer








          New contributor




          Jonathan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.


















          • Tried your code. It's not working. I have updated the code with sample data
            – Annabelle
            5 hours ago













          up vote
          0
          down vote










          up vote
          0
          down vote









          This dont work because the includes dont test for multiple term cases. You didnt say whats inside the items Array but if it is a String you could do this:



          transform(items: any, terms: string): any {
          if(!items) return ;
          if(!terms) return items;
          terms = terms.toLowerCase();
          return items.filter( it => {
          //if it is something like "Programmer 07 November 12:00PM"
          var informations = it.split(' '); //["Programmer", "07" ,"November" ,"12:00PM"]
          var termArray = terms.split(' ');
          var rightResult = true;
          for (var index in termArray) {
          if !(informations.include(termArray[index])) {
          rightResult = false;
          }
          return rightResult;


          });
          }





          share|improve this answer








          New contributor




          Jonathan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          This dont work because the includes dont test for multiple term cases. You didnt say whats inside the items Array but if it is a String you could do this:



          transform(items: any, terms: string): any {
          if(!items) return ;
          if(!terms) return items;
          terms = terms.toLowerCase();
          return items.filter( it => {
          //if it is something like "Programmer 07 November 12:00PM"
          var informations = it.split(' '); //["Programmer", "07" ,"November" ,"12:00PM"]
          var termArray = terms.split(' ');
          var rightResult = true;
          for (var index in termArray) {
          if !(informations.include(termArray[index])) {
          rightResult = false;
          }
          return rightResult;


          });
          }






          share|improve this answer








          New contributor




          Jonathan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          share|improve this answer



          share|improve this answer






          New contributor




          Jonathan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          answered Nov 7 at 7:53









          Jonathan

          12




          12




          New contributor




          Jonathan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.





          New contributor





          Jonathan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.






          Jonathan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.












          • Tried your code. It's not working. I have updated the code with sample data
            – Annabelle
            5 hours ago


















          • Tried your code. It's not working. I have updated the code with sample data
            – Annabelle
            5 hours ago
















          Tried your code. It's not working. I have updated the code with sample data
          – Annabelle
          5 hours ago




          Tried your code. It's not working. I have updated the code with sample data
          – Annabelle
          5 hours ago










          up vote
          0
          down vote













          Inside your transform method of your search pipe, apply filters on all the fields you want to apply filter on. Following will search for all keys in the object:



          transform(items: any, terms: string): any {
          if(!items) return ;
          if(!terms) return items;
          terms = terms.toLowerCase();
          return items.filter( it => {

          return keys(it).reduce((prev, key) => {
          return prev || key.toLowerCase().includes(term);
          }, false);
          });
          }


          If keys or Object.keys are not working, use the following code instead of reduce function:



          ...
          let bInclude = false;
          for(let key in it){
          bInclude = bInclude || key.toLowerCase().includes(term);
          }

          return bInclude;
          ...





          share|improve this answer























          • ERROR ReferenceError: keys is not defined @Shubham Chaudhary
            – Annabelle
            Nov 8 at 4:57










          • Use Object.keys instead. @Annabelle
            – Shubham Chaudhary
            Nov 8 at 17:17










          • Tried that but it's still not working @Shubham Chaudhary
            – Annabelle
            Nov 9 at 6:50












          • Edited the answer, use for..in for getting object keys as array
            – Shubham Chaudhary
            Nov 9 at 19:13










          • Tried your code. Its still not working. I have sample data and updated the question.
            – Annabelle
            5 hours ago















          up vote
          0
          down vote













          Inside your transform method of your search pipe, apply filters on all the fields you want to apply filter on. Following will search for all keys in the object:



          transform(items: any, terms: string): any {
          if(!items) return ;
          if(!terms) return items;
          terms = terms.toLowerCase();
          return items.filter( it => {

          return keys(it).reduce((prev, key) => {
          return prev || key.toLowerCase().includes(term);
          }, false);
          });
          }


          If keys or Object.keys are not working, use the following code instead of reduce function:



          ...
          let bInclude = false;
          for(let key in it){
          bInclude = bInclude || key.toLowerCase().includes(term);
          }

          return bInclude;
          ...





          share|improve this answer























          • ERROR ReferenceError: keys is not defined @Shubham Chaudhary
            – Annabelle
            Nov 8 at 4:57










          • Use Object.keys instead. @Annabelle
            – Shubham Chaudhary
            Nov 8 at 17:17










          • Tried that but it's still not working @Shubham Chaudhary
            – Annabelle
            Nov 9 at 6:50












          • Edited the answer, use for..in for getting object keys as array
            – Shubham Chaudhary
            Nov 9 at 19:13










          • Tried your code. Its still not working. I have sample data and updated the question.
            – Annabelle
            5 hours ago













          up vote
          0
          down vote










          up vote
          0
          down vote









          Inside your transform method of your search pipe, apply filters on all the fields you want to apply filter on. Following will search for all keys in the object:



          transform(items: any, terms: string): any {
          if(!items) return ;
          if(!terms) return items;
          terms = terms.toLowerCase();
          return items.filter( it => {

          return keys(it).reduce((prev, key) => {
          return prev || key.toLowerCase().includes(term);
          }, false);
          });
          }


          If keys or Object.keys are not working, use the following code instead of reduce function:



          ...
          let bInclude = false;
          for(let key in it){
          bInclude = bInclude || key.toLowerCase().includes(term);
          }

          return bInclude;
          ...





          share|improve this answer














          Inside your transform method of your search pipe, apply filters on all the fields you want to apply filter on. Following will search for all keys in the object:



          transform(items: any, terms: string): any {
          if(!items) return ;
          if(!terms) return items;
          terms = terms.toLowerCase();
          return items.filter( it => {

          return keys(it).reduce((prev, key) => {
          return prev || key.toLowerCase().includes(term);
          }, false);
          });
          }


          If keys or Object.keys are not working, use the following code instead of reduce function:



          ...
          let bInclude = false;
          for(let key in it){
          bInclude = bInclude || key.toLowerCase().includes(term);
          }

          return bInclude;
          ...






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 9 at 19:12

























          answered Nov 7 at 17:02









          Shubham Chaudhary

          265




          265












          • ERROR ReferenceError: keys is not defined @Shubham Chaudhary
            – Annabelle
            Nov 8 at 4:57










          • Use Object.keys instead. @Annabelle
            – Shubham Chaudhary
            Nov 8 at 17:17










          • Tried that but it's still not working @Shubham Chaudhary
            – Annabelle
            Nov 9 at 6:50












          • Edited the answer, use for..in for getting object keys as array
            – Shubham Chaudhary
            Nov 9 at 19:13










          • Tried your code. Its still not working. I have sample data and updated the question.
            – Annabelle
            5 hours ago


















          • ERROR ReferenceError: keys is not defined @Shubham Chaudhary
            – Annabelle
            Nov 8 at 4:57










          • Use Object.keys instead. @Annabelle
            – Shubham Chaudhary
            Nov 8 at 17:17










          • Tried that but it's still not working @Shubham Chaudhary
            – Annabelle
            Nov 9 at 6:50












          • Edited the answer, use for..in for getting object keys as array
            – Shubham Chaudhary
            Nov 9 at 19:13










          • Tried your code. Its still not working. I have sample data and updated the question.
            – Annabelle
            5 hours ago
















          ERROR ReferenceError: keys is not defined @Shubham Chaudhary
          – Annabelle
          Nov 8 at 4:57




          ERROR ReferenceError: keys is not defined @Shubham Chaudhary
          – Annabelle
          Nov 8 at 4:57












          Use Object.keys instead. @Annabelle
          – Shubham Chaudhary
          Nov 8 at 17:17




          Use Object.keys instead. @Annabelle
          – Shubham Chaudhary
          Nov 8 at 17:17












          Tried that but it's still not working @Shubham Chaudhary
          – Annabelle
          Nov 9 at 6:50






          Tried that but it's still not working @Shubham Chaudhary
          – Annabelle
          Nov 9 at 6:50














          Edited the answer, use for..in for getting object keys as array
          – Shubham Chaudhary
          Nov 9 at 19:13




          Edited the answer, use for..in for getting object keys as array
          – Shubham Chaudhary
          Nov 9 at 19:13












          Tried your code. Its still not working. I have sample data and updated the question.
          – Annabelle
          5 hours ago




          Tried your code. Its still not working. I have sample data and updated the question.
          – Annabelle
          5 hours ago


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53185106%2ffilter-list-based-on-more-than-one-field%23new-answer', 'question_page');
          }
          );

          Post as a guest




















































































          這個網誌中的熱門文章

          Academy of Television Arts & Sciences

          L'Équipe

          1995 France bombings