Filter array of objects whose any properties contains a value

后端 未结 8 1106
你的背包
你的背包 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:59

    function filterByValue(arrayOfObject,words){
      let reg = new RegExp(words,'i');
      return arrayOfObject.filter((item)=>{
         let flag = false;
         for(prop in item){
           if(reg.test(prop)){
              flag = true;
           }  
         }
         return flag;
      });
    }

提交回复
热议问题