texas697 texas697 - 1 year ago 154
Javascript Question

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?

Answer Source

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);

Sorry if that's not what you were asking :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download