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 = ['plete', 'inProgress', 'notStarted']
How can i filter without using multiple if/else statements?
let subdivisions = realm.objects('Subdivision');
let result = subdivisions.filtered('statusSurvey == $0',['plete','notStarted']);
Or is it necessary to use plain javascript or lodash?
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 = ['plete', 'inProgress', 'notStarted']
How can i filter without using multiple if/else statements?
let subdivisions = realm.objects('Subdivision');
let result = subdivisions.filtered('statusSurvey == $0',['plete','notStarted']);
Or is it necessary to use plain javascript or lodash?
Share Improve this question asked Jul 12, 2016 at 0:54 texas697texas697 6,47719 gold badges70 silver badges136 bronze badges2 Answers
Reset to default 4Here'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)
Are you asking how to dynamically build a realm query string that would return results based off the current filters?
If I'm understanding correctly, you could build your query string like:
var query = 'statusSurvey == ';
for (var i = 0; i < filters.length; i++) {
query += `'${filters[i]}'`;
if (i + 1 < filters.length) {
query += ` OR statusSurvey == `
}
}
var result = subdivisions.filtered(query);