Javascript :Dynamic expression in filter function based on variable values

陌路散爱 提交于 2019-12-11 23:56:56

问题


I am trying to implement a filtering functionality for showing list of buses based on some parameters using lodash.js.

Here are four parameters for filtering boardingPoint,droppingPoint,busType and operatorName and it will be populated in 4 dropdown menu's.

Workflow

When user select a boardingPoint the result should contains only list of buses with selected boarding points ,

If he select boardingPoint and droppingPoint result should contains only the list of buses with selected boradingPoint and dropping Points and so on.

Here is my function for filtering

    function search_buses(bpLocations,dpLocations,busTypes,operatorNames){

    //filter function 
    tresult = _.filter(result, function(obj) {

                    return _(obj.boardingPoints).map('location').intersection(bpLocations).value().length > 0
                        &&_(obj.droppingPoints).map('location').intersection(dpLocations).value().length > 0
                        && _.includes(busTypes, obj.busType)
                        && _.includes(operatorNames, obj.operatorName);
                      });
     //return result array
     return tresult;
}

But the problem is if user selects only two items say boarding and dropping points and others are empty in that case the above filter condition failed,because it will evaluate four conditions. The above will works only if all the 4 parameters have any value.

So how can i modify the above expression it should contains only selected parameter for filtering

Eg:if user selects boarding point and dropping point (ie bpLocations,dpLocations ) only expression should be this

 tresult = _.filter(result, function(obj) {                  
                    return _(obj.boardingPoints).map('location').intersection(bpLocations).value().length > 0
                   &&_(obj.droppingPoints).map('location').intersection(dpLocations).value().length > 0
});

if selects only busType it should be

tresult = _.filter(result, function(obj) {
                return  _.includes(busTypes, obj.busType)
                  });

UPDATE

I just concatenate the expression string based on each variable is empty or not

//expression string
var finalvar;
 tresult = _.filter(result, function(obj) {
                    if(bpLocations!=''){
                     finalvar+=_(obj.boardingPoints).map('location').intersection(bpLocations).value().length > 0;
                    }
                    if(dpLocations!=''){
                        finalvar+=&&+_(obj.droppingPoints).map('location').intersection(dpLocations).value().length > 0;
                    }
                    if(busTypes!=''){
                        finalvar+=&&+ _.includes(busTypes, obj.busType);
                    }
                    if(operatorNames!=''){
                        finalvar+=&&+ _.includes(operatorNames, obj.operatorName);
                    }
                   return finalvar;
              });

But it will returns this error Uncaught SyntaxError: Unexpected token &&


回答1:


First of all, finalvar needs to be initialized. It should be initialized to true if you want no filter at all when nothing is selected which I'm assuming that it's the logic you want.

Second, addition assignment '+=' is incorrectly used in this case. Please check here for correct usage.

Third, && is JavaScript operator, it can't be added to another value. That's why you're getting the error.

Here is the revised code which would fix the problems and the expected logic:

// initialize to true means no filter if nothing is selected (return the item).  
var finalvar = true;

var tresult = _.filter(result, function(obj) {
    if(bpLocations){
        finalvar = finalvar && _(obj.boardingPoints).map('location').intersection(bpLocations).value().length > 0;
    }
    if(dpLocations){
        finalvar = finalvar && _(obj.droppingPoints).map('location').intersection(dpLocations).value().length > 0;
    }
    if(busTypes){
        finalvar = finalvar && _.includes(busTypes, obj.busType);
    }
    if(operatorNames){
        finalvar = finalvar && _.includes(operatorNames, obj.operatorName);
    }
    return finalvar;
});


来源:https://stackoverflow.com/questions/35954304/javascript-dynamic-expression-in-filter-function-based-on-variable-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!