简体   繁体   中英

Javascript :Dynamic expression in filter function based on variable values

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 &&

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM