将字符串解析为javaScript中的条件? [英] Parse a string into conditions in javaScript?

查看:90
本文介绍了将字符串解析为javaScript中的条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一些条件的字符串,例如:

I have a string containing some conditions, for example:

var str =this.demoModel.active =='1'& ;& this.demoModel.span> 5 || ...

是否有直接的方法在javascript中解析它们他们的工作就像一系列条件。类似于:

Is there a direct way in javascript to parse them so that, they work like a set of conditions. Something like:

if(JSON.parse(str){})。 ??

推荐答案

一般来说,你应该尽量避免陷入这种情况:将JavaScript存储在字符串中供以后评估应该是尽可能避免。根据您的实际情况,您可以考虑以下选项:

In general you should try to avoid getting into this situation at all: storing JavaScript in strings for later evaluation should be avoided if possible. Depending on your actual case you could consider the following option:

它们在实际使用中受到限制,因为它们与使用它们的脚本一起解析,但他们也很安全

They are limited in practical use, since they are parsed together with the script in which they are used, but they are also safe:

var str =  `${this.demoModel.active == '1' && this.demoModel.span > 5}`;

分配此字符串后,它会立即评估 $ {} 其中的部分。

When this string is assigned, it will immediately evaluate the ${ } parts in it.

因此,如果评估可以立即发生,这只是一个解决方案,因为您无法将其存储在字符串中然后期望稍后触发评估。

This is thus only a solution if the evaluation can happen immediately, because you cannot store this in a string and then expect to trigger the evaluation later.

因此与以下内容没有太大区别:

And so it is not much different from:

var bool = this.demoModel.active == '1' && this.demoModel.span > 5;



2。通过回调延迟评估



解决方法可能是定义一个评估模板文字或表达式的函数,如下所示:

2. Deferred evaluation through callback

A work-around could be to define a function that evaluates the template literal or expression, like so:

var rule = function() { 
    return this.demoModel.active == '1' && this.demoModel.span > 5;
};

...你可以传递该函数,例如作为回调:

... and you can pass that function around, for instance as a callback:

doSomething(rule);

...然后 doSomething 可以像这样调用它,绑定上下文,以便具有适当的值:

... and then doSomething could call it like this, binding the context, so that this has the appropriate value:

function doSomething(rule) {
    if (rule.call(this)) console.log('rule passed');
}



3。嵌套对象数据结构



另一种选择是为表达式创建一个对象结构,例如:

3. Nested Object Data Structure

Another option would be to create an object structure for the expressions, for example like this:

var rules = [
    [{ // AND conditions:
        "field": "active",
        "compare": "eq",
        "target": 1
    }, {
        "field": "span",
        "compare": "gt",
        "target": 5
    }], // Next array will be OR'ed
    [{
        "field": "...."
        "compare": "..",
        "target": ...
    }]
}];

这是一个嵌套数组,其中顶层将具有必须一起进行OR运算的规则,而内部级别将与AND在一起。

This is a nested array, where the top level will have rules that must be OR'ed together, while the inner level will be AND'ed together.

然后编写一个将处理此结构的函数。 compare 名称可以映射到代码中的函数,如下所示:

Then write a function that will process this structure. The compare names could be mapped to functions in your code, like this:

const comparators = {
    "eq": (a, b) = a === b,
    "gt": (a, b) = a > b
};

因此,要评估规则数组中的一个对象,可以使用此:

So to evaluate one object in the rules array, you could use this:

execute: (obj) => comparators[this.demoModel[obj.compare]] // get the function
    (this.demoModel[obj.field], obj.target) // pass arguments

这个规则结构可以很容易地保存并加载为JSON字符串。

This rules structure can easily be saved and loaded as JSON string.

这篇关于将字符串解析为javaScript中的条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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