Javascript:基于变量值的过滤器函数中的动态表达式 [英] Javascript :Dynamic expression in filter function based on variable values

查看:101
本文介绍了Javascript:基于变量值的过滤器函数中的动态表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现过滤功能,以使用lodash.js根据某些参数显示总线列表.

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

这里有四个用于过滤boardingPoint,droppingPoint,busType和operatorName的参数,它将填充在四个下拉菜单中.

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

工作流程

当用户选择一个登机点时,结果应仅包含具有选定登机点的公交车列表,

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

如果他选择boardingPoint和droppingPoint,则结果应仅包含具有选择的boradingPoint和dropping Points的公交车列表.

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

这是我的过滤功能

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

但是问题是,如果用户仅选择两个项目,例如登机点和落点,而在上述情况下,其他条件为空,则上述筛选条件失败,因为它将评估四个条件. 仅当所有4个参数均具有任何值时,以上方法才有效.

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

例如:如果用户选择登机点和落机点(即bpLocationsdpLocations),则表达式应为

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

如果仅选择busType,则应为

if selects only busType it should be

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

更新

我只是根据每个变量是否为空来连接表达式字符串

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

但是它将返回此错误Uncaught SyntaxError: Unexpected token &&

推荐答案

首先,需要初始化finalvar.如果在没有选择任何内容的情况下根本不需要过滤器(我假设这是您想要的逻辑),则应将其初始化为true.

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.

第三,&&是JavaScript运算符,无法将其添加到另一个值.这就是为什么您会收到错误消息.

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

这篇关于Javascript:基于变量值的过滤器函数中的动态表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆