Filter array of objects whose any properties contains a value

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

    Well when we already know that its not going to be a search on an object with methods, we can do the following for saving bit on time complexity :

    function filterByValue(array, value) {
      return array.filter((data) =>  JSON.stringify(data).toLowerCase().indexOf(value.toLowerCase()) !== -1);
    }
    

提交回复
热议问题