0

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 Answer 1

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;
});
Sign up to request clarification or add additional context in comments.

6 Comments

Its worked partially ,sometimes it returns empty array even if the filter condition exists.
If you can explain the condition that doesn't work, then maybe I can figure out why. Note that these filters are case sensitive.
each parameters is listed in drop down menu eg: operatorname listed as like this way a,b,c,d,e,f (Assume this is the values listed in operatorname dropdown list) if the the operatorNames array in the order same as the dropown (ie operatorNames=[a,b,c,d] ) it will work but if i randomly select one ( ie operatorNames=[d]) it will returns empty array.
The way _.includes() works is the 1st params is any array, the 2nd is a string. So when you have _.include(operatorNames, obj.operatorName), operatorNames = ['d'], it only returns the item only if you have obj.operatorName = 'd'. Anything else other than 'd' for obj.operatorName will return empty.
I think its problem with the condition.are you sure your logic is correct
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.