Filter array of objects whose any properties contains a value

后端 未结 8 1089
你的背包
你的背包 2020-12-13 19:26

I\'m wondering what is the cleanest way, better way to filter an array of objects depending on a string keyword. The search has to be made in any properties of

8条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-13 19:39

    Use Object.keys to loop through the properties of the object. Use reduce and filter to make the code more efficient:

     const results = arrayOfObject.filter((obj)=>{
         return Object.keys(obj).reduce((acc, curr)=>{
               return acc || obj[curr].toLowerCase().includes(term);
         }, false);
    }); 
    

    Where term is your search term.

提交回复
热议问题