Skip to content

Instantly share code, notes, and snippets.

@swateek
Created September 14, 2017 06:21
Show Gist options
  • Save swateek/eaf7382e0695da55ae7280fa0edf1ac2 to your computer and use it in GitHub Desktop.
Save swateek/eaf7382e0695da55ae7280fa0edf1ac2 to your computer and use it in GitHub Desktop.
Using multiple filter fields and filter data with REST
var params = {};
var filter = {};
params['filterField'] = "field1,field2";
params['filterData'] = "2,3;abc,cde";
if(params.hasOwnProperty('filterField') && params.hasOwnProperty('filterData')){
var filterFieldArr = params.filterField.split(',');
var filterDataArr = params.filterData.split(';');
if(filterFieldArr.length == filterDataArr.length){
for(var i=0; i<filterFieldArr.length; i++){
filter[filterFieldArr[i]] = {};
if(filterDataArr[i].indexOf(',') > -1){ // has multiple data for one field.
var itm = filterDataArr[i].split(",");
if(filterFieldArr[i] == "field1"){ // special treatment
filter[filterFieldArr[i]] = {'$lte': itm[0], '$gte': itm[1]}
}else{
filter[filterFieldArr[i]] = {'$in': itm}
}
}else{
filter[filterFieldArr[i]] = filterData[i];
}
}
}
}
console.log(filter);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment