【问题标题】:Javascript :Dynamic expression in filter function based on variable valuesJavascript:基于变量值的过滤函数中的动态表达式
【发布时间】:2016-03-12 05:53:21
【问题描述】:

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

这里有 4 个参数用于过滤 boardingPoint、droppingPoint、busType 和 operatorName,它将被填充到 4 个下拉菜单中。

工作流程

当用户选择一个登机点时,结果应该只包含带有选定登机点的巴士列表,

如果他选择了上车点和下车点,结果应该只包含选择了上车点和下车点等的巴士列表。

这是我的过滤功能

    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 个参数都具有任何值时,上述方法才有效。

那么我该如何修改上面的表达式,它应该只包含用于过滤的选定参数

例如:如果用户选择登机点和下机点(即bpLocations,dpLocations),则只有表达式应该是这个

 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 它应该是

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

更新

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

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

【问题讨论】:

    标签: javascript jquery html lodash


    【解决方案1】:

    首先需要初始化finalvar。如果您在未选择任何内容时根本不需要过滤器,则应将其初始化为 true,我假设这是您想要的逻辑。

    其次,在这种情况下错误地使用了加法赋值“+=”。请检查here的正确用法。

    第三,&& 是 JavaScript 操作符,它不能被添加到另一个值。这就是您收到错误消息的原因。

    这里是修改后的代码,可以解决问题和预期的逻辑:

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

    【讨论】:

    • 它部分工作,有时即使过滤条件存在,它也会返回空数组。
    • 如果你能解释不起作用的情况,那么也许我能找出原因。请注意,这些过滤器区分大小写。
    • 每个参数都列在下拉菜单中,例如:operatorname 这样列出 a,b,c,d,e,f(假设这是 operatorname 下拉列表中列出的值)如果 operatorNames 数组的顺序相同作为下拉菜单(即operatorNames=[a,b,c,d]),它会起作用,但如果我随机选择一个(即operatorNames=[d]),它将返回空数组。
    • _.includes() 的工作方式是第一个参数是任何数组,第二个是字符串。所以当你有 _.include(operatorNames, obj.operatorName), operatorNames = ['d'] 时,只有当你有 obj.operatorName = 'd' 时它才会返回项目。 obj.operatorName 除了 'd' 之外的任何其他内容都将返回空。
    • 我认为它的条件问题。你确定你的逻辑是正确的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-08
    相关资源
    最近更新 更多