最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to dynamically filter with react-native realm - Stack Overflow

programmeradmin5浏览0评论

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 badges
Add a ment  | 

2 Answers 2

Reset to default 4

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)

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); 
发布评论

评论列表(0)

  1. 暂无评论