简体   繁体   中英

Typescript dynamic filtering array of objects

I have array of objects, I want to make a dynamic filter which means just pass a value without key to search every key value on object and returns the matched objects. Same as the angular material table which filter all column

[
   {
      "id":4,
      "email":"eve.holt@reqres.in",
      "first_name":"Eve",
      "last_name":"Holt",
      "avatar":"https://s3.amazonaws.com/uifaces/faces/twitter/marcoramires/128.jpg",
      "date":"2020-02-24T13:19:08.630025Z"

},
   {
      "id":5,
      "email":"charles.morris@reqres.in",
      "first_name":"Charles",
      "last_name":"Morris",
      "avatar":"https://s3.amazonaws.com/uifaces/faces/twitter/stephenmoon/128.jpg",
      "date":"2020-02-24T13:19:08.630025Z"

},
   {
      "id":6,
      "email":"tracey.ramos@reqres.in",
      "first_name":"Tracey",
      "last_name":"Ramos",
      "avatar":"https://s3.amazonaws.com/uifaces/faces/twitter/bigmancho/128.jpg",
      "date":"2020-02-24T13:19:08.630025Z"

}
]

Also I want to be able to filter on date

You can do it like this, assuming data contains your array of objects.
filtered contains the results with the matched objects.

const search = 'yourValue';
const filtered = data.filter(obj => {
  return !!JSON.stringify(Object.values(obj)).match(new RegExp(search, 'i'));
});

You can use Lunr for a lightweight full text search implementation. For date filtering you can use Crossfilter .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM