Created
September 14, 2017 06:21
-
-
Save swateek/eaf7382e0695da55ae7280fa0edf1ac2 to your computer and use it in GitHub Desktop.
Using multiple filter fields and filter data with REST
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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