javascript - In an array of objects, returns objects where ANY value matches a specific string

后端 未结 5 959
清酒与你
清酒与你 2020-12-04 00:56

I\'m implementing search functionality into my application. The search results in the UI are returned based on an array of objects. Essentially what I\'m trying to do is ite

5条回答
  •  南笙
    南笙 (楼主)
    2020-12-04 01:13

    function searchObj(search){
      let answer = [];
      result.forEach(re => {
        if(JSON.stringify(re).indexOf(search) > 0){
          answer.push(re)
        }
      });
      return answer;
    }
    

    Loop through every element of the array, convert them into a string and use indexOf to find the matching criteria. That way you can save a few loops without looping each and every key of every element.

提交回复
热议问题