[英]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.