Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to dynamically filter with react-native realm

I am switching to realm for react native and would like to make use of their built in queries/filtering. I have checkbox filters with 3 possible options, the selected are passed as a array:

var filters = ['complete', 'inProgress', 'notStarted']

How can i filter without using multiple if/else statements?

  let subdivisions = realm.objects('Subdivision');
       let result = subdivisions.filtered('statusSurvey == $0',['complete','notStarted']);

Or is it necessary to use plain javascript or lodash?

like image 264
texas697 Avatar asked Sep 21 '26 06:09

texas697


1 Answers

Here's another way to do it without building the query in a string, and having to escape the filter values:

filters = ['a', 'b', 'c']
const realmFilter = [Array(filters.length).fill().map((x, i)=> `statusSurvey == $${i}`).join(" OR ")].concat(filters)

realm.objects('Subdivision').filtered(...realmFilter)
like image 178
Onno Faber Avatar answered Sep 22 '26 18:09

Onno Faber



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!