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?
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With