带有多个变量的 javascript eval

发布于 01-15 14:43 字数 179 浏览 3 评论 0原文

我想使用 eval 计算以下具有多个变量的字符串 我将所有变量存储在我的对象中

let myVars = {
    a : 10,
    b : 20,
    c : 30,
}
let str = "a+b+c+10"
// how to evaluate the string ...

I want to use eval to calculate the following string with multiple varaibles
I have all the variables stored in my object

let myVars = {
    a : 10,
    b : 20,
    c : 30,
}
let str = "a+b+c+10"
// how to evaluate the string ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

萝莉病2025-01-22 14:43:38

您可以替换(未知)变量并使用 eval(洞穴!)以获得结果。

let
    values = { a: 10, b: 20, c: 30 },
    str = "a+b+c+10";

str = str.replace(/[a-z0-9_]+/g, s => values[s] ?? s);

console.log(eval(str));

You could replace the (unknown) variables and use eval (cave!) for getting a result.

let
    values = { a: 10, b: 20, c: 30 },
    str = "a+b+c+10";

str = str.replace(/[a-z0-9_]+/g, s => values[s] ?? s);

console.log(eval(str));

怀念你的温柔2025-01-22 14:43:38

我不确定我是否理解你的问题,但我会尽力回答。

eval() 方法会自动从全局上下文中获取变量。如果您想获得结果,只需保存 eval() 函数的结果即可。

// Object destructuring for easier readability
let { a, b, c } = {
  a : 10,
  b : 20,
  c : 30,
}
let str = "a + b + c + 10";

const total = eval(str);
console.log(total); // Should be: 70

上面的代码将返回数学运算的结果。

但是,您应该永远不要使用eval(),因为它可以任意运行任何代码。

I'm not sure if I understood your question, but I will try my best to answer.

The eval() method will automatically get the variables from the global context. If you want to get the result, just save the result from the eval() function.

// Object destructuring for easier readability
let { a, b, c } = {
  a : 10,
  b : 20,
  c : 30,
}
let str = "a + b + c + 10";

const total = eval(str);
console.log(total); // Should be: 70

The code above will return the result from the mathematical operation.

However, you should never use eval(), as it can run any code arbitrarily.

赠意2025-01-22 14:43:38

您必须提到 a+b+c... 用于 myVars 对象,因此您有不同的方法:

let myVars = {
    a : 10,
    b : 20,
    c : 30,
}
let str = "myVars.a+myVars.b+myVars.c+10"
console.log(eval(str))

虽然我不建议你这种方式,因为这里我们可以使用它而不需要 eval,这样会更容易。

另一种方法是使用正则表达式:

let myVars = {
    a : 10,
    b : 20,
    c : 30,
}
let str = "a+b+c+10"
const arrayName="myVars"
str=str.replace(/([a-z]+)/gi,arrayName+".$1")
console.log(eval(str))

在这里,您可以使用正则表达式在变量后面动态添加对象名称。

或者简单地你可以做你想做的事情而无需评估(如果你愿意):

let myVars = {
    a : 10,
    b : 20,
    c : 30,
}

let sum=Object.values(myVars).reduce((partialSum, a) => partialSum + a, 0) + 10;
console.log(sum)

You have to mention that a+b+c... are for the myVars object so you have different ways:

let myVars = {
    a : 10,
    b : 20,
    c : 30,
}
let str = "myVars.a+myVars.b+myVars.c+10"
console.log(eval(str))

Although I don't suggest this way to you because here we can use that without eval so that will be easier.

Another way is to use Regex:

let myVars = {
    a : 10,
    b : 20,
    c : 30,
}
let str = "a+b+c+10"
const arrayName="myVars"
str=str.replace(/([a-z]+)/gi,arrayName+".$1")
console.log(eval(str))

Here you add the object name behind the variables dynamically with Regex.

Or simply you can do what you want without eval (If you want):

let myVars = {
    a : 10,
    b : 20,
    c : 30,
}

let sum=Object.values(myVars).reduce((partialSum, a) => partialSum + a, 0) + 10;
console.log(sum)

胡渣熟男2025-01-22 14:43:38

您可以使用 ma​​thjs 库代替。

首先下载库:

npm install mathjs

导入评估(),然后简单地使用评估(表达式,范围),如下所示:

let myVars = {
  a : 10,
  b : 20,
  c : 30,
}
let str = "a+b+c+10"
console.log(evaluate(str, myVars)) # Prints 70

查看文档以获取更多示例:mathjs

You can use mathjs library instead.

First download the library:

npm install mathjs

Import evaluate(), then simply use evaluate(expression, scope) like this:

let myVars = {
  a : 10,
  b : 20,
  c : 30,
}
let str = "a+b+c+10"
console.log(evaluate(str, myVars)) # Prints 70

Check the documentation for more examples: mathjs

权谋诡计2025-01-22 14:43:38

虽然Eval 是邪恶的,因为它允许执行任意代码,但您可以使用with在这里改变eval 语句的全局命名空间:

let myVars = {
  a: 10,
  b: 20,
  c: 30,
};
let str = "a+b+c+10";
let result = null;
with (myVars) {
  result = eval(str);
}
console.log(result); // logs 70

再次强调,我不建议使用eval

While remembering that Eval is Evil since it allows for arbitrary code execution, you can use the with block here to change the eval statement's global namespace:

let myVars = {
  a: 10,
  b: 20,
  c: 30,
};
let str = "a+b+c+10";
let result = null;
with (myVars) {
  result = eval(str);
}
console.log(result); // logs 70

Again, I do not recommend using eval.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文