简体   繁体   English

将字符串转换为比较运算符。 可能吗? 使用Javascript

[英]converting string into comparison operator. Possible or not? Javascript

I'm creating a function to filter my javascript objects. 我正在创建一个函数来过滤我的javascript对象。

function filter(array,filterName,filterCondition,filterParameter){

    for(var i=0; i< array.length;i++){
        if(array[i][filterName] -<convert FilterCondition here>- filterParameter){

        }
    }

}

and ideally I would like to use it like this: 理想情况下,我想这样使用它:

filter(anArray,"age",">",10)

Is it possible to convert a string comparison operator into a real operator in my if statement? 是否可以在if语句中将字符串比较运算符转换为实运算符?

For example, you can use hash like this: 例如,您可以使用如下哈希:

function filter(array,filterName,filterCondition,filterParameter){
    var comparisonOperatorsHash = {
        '<': function(a, b) { return a < b; },
        '>': function(a, b) { return a > b; },
        '>=': function(a, b) { return a >= b; },
        '<=': function(a, b) { return a <= b; },
        '==': function(a, b) { return a == b; },
        '===': function(a, b) { return a === b; },
    };

    var comparisonOperator = comparisonOperatorsHash[filterCondition];

    if (comparisonOperator === undefined) {
        // throw error here
    }

    for(var i=0; i< array.length;i++){
        if(comparisonOperator(array[i][filterName], filterParameter)) {

        }
    }
}

eval is evil, but can do anything. eval是邪恶的,但无能为力。 It's considered evil if arbitrary input can be passed to it, like user input. 如果可以将任意输入(如用户输入)传递给它,则认为是邪恶的。 But if you control what's in there, it should be safe. 但是,如果您控制其中的内容,那应该是安全的。 More info here . 更多信息在这里

So you could do: 因此,您可以执行以下操作:

 function filter(array,filterName,filterCondition,filterParameter){ var results = []; for(var i=0; i< array.length;i++){ if(eval(array[i][filterName] + filterCondition + filterParameter)){ results.push(array[i]['name']) } } document.body.innerHTML = 'These guys are older than 10: ' + results.join(', '); } var anArray = [ {'name': 'Patrick', 'age': 8 }, {'name': 'John', 'age': 12 }, {'name': 'Debora', 'age': 26 }, {'name': 'Jenna', 'age': 3 }, {'name': 'Brandon', 'age': 14 }, ]; filter(anArray,"age",">",10); 

Edit 编辑

I've thought of another way without the eval , or an endless switch statement. 我想到了没有eval或无休止的switch语句的另一种方法。 The trick here is to create a temporary script tag, that will hold a function to check conditions. 这里的技巧是创建一个临时script标签,该标签将包含一个检查条件的函数。 This will avoid recompiling your function on every iteration of the loop, as it would with eval . eval一样,这将避免在循环的每次迭代中重新编译eval

 var conditionScript = document.createElement('script'); function filter(array,filterName,filterCondition,filterParameter){ var results = []; prepareCondition(filterCondition,filterParameter); for(var i=0; i< array.length;i++){ if( evalCondition(array[i][filterName]) ){ results.push(array[i]['name']); } } document.body.innerHTML = 'These guys are older than 10: ' + results.join(', '); } function prepareCondition(filterCondition,filterParameter){ if(conditionScript.parentNode) document.body.removeChild(conditionScript); conditionScript.innerText = 'function evalCondition(value){' + ' return value ' + filterCondition + filterParameter + '}'; document.body.appendChild(conditionScript); } var anArray=[ {'name': 'Patrick', 'age': 8 }, {'name': 'John', 'age': 12 }, {'name': 'Debora', 'age': 26 }, {'name': 'Jenna', 'age': 3 }, {'name': 'Brandon', 'age': 14 }, ]; filter(anArray,"age",">",10); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 .apply() 与 &#39;new&#39; 运算符一起使用。 这可能吗? - Use of .apply() with 'new' operator. Is this possible? Javascript将字符串计算为比较运算符 - Javascript evaluate string as a comparison operator JavaScript传播运算符。 {…obj}和obj有什么区别 - Javascript spread operator. What's the difference between { … obj } and obj Javascript函数 - 将字符串参数转换为运算符 - Javascript function - converting string argument to operator 比较运算符不适用于 JavaScript 中的 0 - Comparison operator is not working on 0 in JavaScript JavaScript比较运算符“不大” - Javascript comparison operator “not bigger” 奇怪的JavaScript比较运算符 - Strange JavaScript comparison operator 将数学运算符作为字符串参数转换为JavaScript中的实数运算符 - Converting mathematical operator as string argument to real operator in javascript 有什么方法可以将字符串用作Javascript或jQuery中的比较运算符? - Is there any way to use a string as a comparison operator in Javascript or with jQuery? 为什么在使用比较运算符与使用!!时将整数转换为真实值的工作方式不同! 在JavaScript中 - Why converting an integer to truthy value works differently when using comparison operator vs using !! in JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM