This question already has an answer here:
这个问题在这里已有答案:
- Are Variable Operators Possible? 6 answers
变量运算符可能吗? 6个答案
If I've got a string that is a mathematic equation and I want to split it and then calculate it. I know I can use the eval() function to do this, but I'm interested if there's an alternative way to do this - specifically by splitting the strings first. So I've got something like
如果我有一个数学方程式的字符串,我想拆分然后计算它。我知道我可以使用eval()函数来做到这一点,但我很感兴趣,如果有另一种方法可以做到这一点 - 特别是首先拆分字符串。所以我有类似的东西
var myString = "225 + 15 - 10"
var newString = myString.split(" ");
This would turn myString into an array: ["225", "+", "15", "-", "10"];
这会将myString变成一个数组:[“225”,“+”,“15”,“ - ”,“10”];
My next task is to turn all the odd-numbered strings into integers, which I think I could use parseInt(); for. My question is, how do I turn the "+" and "-" into actual arithmetic operators? So that at the end I am left with a mathematic expression which I can calculate?
我的下一个任务是将所有奇数编号的字符串转换为整数,我认为我可以使用parseInt();对于。我的问题是,如何将“+”和“ - ”转换为实际的算术运算符?那么最后我留下了一个我可以计算的数学表达式?
Is this possible?
这可能吗?
3 个解决方案
#1
57
var math_it_up = {
'+': function (x, y) { return x + y },
'-': function (x, y) { return x - y }
};
math_it_up['+'](1, 2) == 3;
#2
43
JavaScript's eval
function was mentioned in a comment by Oliver Morgan and I just wanted to flush out how you could use eval
as a solution to your problem.
在Oliver Morgan的评论中提到了JavaScript的eval函数,我只想清除如何使用eval作为解决问题的方法。
The eval
function takes a string and then returns the value of that string considered as a math operation. For example,
eval函数接受一个字符串,然后返回被视为数学运算的字符串的值。例如,
eval("3 + 4")
will return 7 as 3 + 4 = 7.
将返回7为3 + 4 = 7。
This helps in your case because you have an array of strings that you want you want to treat as the mathematical operators and operands that they represent.
这有助于您的情况,因为您有一个字符串数组,您希望将它们视为它们所代表的数学运算符和操作数。
myArray = ["225", "+", "15", "-", "10"];
Without converting any of the strings in your array into integers or operators you could write
不将数组中的任何字符串转换为可以编写的整数或运算符
eval(myArray[0] + myArray[1] + myArray[2]);
which will translate to
这将转化为
eval("225" + "+" + "15");
which will translate to
这将转化为
eval("225 + 15");
which will return 240, the value of adding 225 to 15.
这将返回240,将值225添加到15。
Or even more generic:
甚至更通用:
eval(myArray.join(' '));
which will translate to
这将转化为
eval("225 + 15 - 10");
which will return 230, the value of adding 15 to 225 and substract 10. So you can see that in your case it may be possible to skip converting the strings in your array into integers and operators and, as Oliver Morgan said, just eval
it.
这将返回230,增加15到225和减去10的值。所以你可以看到,在你的情况下,可以跳过将数组中的字符串转换为整数和运算符,正如Oliver Morgan所说,只是评估它。
#3
0
Consider using isNaN(Number(array[x]))
and a switch
for anything that doesn't satisfy the condition.
考虑使用isNaN(Number(array [x]))和一个不满足条件的开关。
#1
57
var math_it_up = {
'+': function (x, y) { return x + y },
'-': function (x, y) { return x - y }
};
math_it_up['+'](1, 2) == 3;
#2
43
JavaScript's eval
function was mentioned in a comment by Oliver Morgan and I just wanted to flush out how you could use eval
as a solution to your problem.
在Oliver Morgan的评论中提到了JavaScript的eval函数,我只想清除如何使用eval作为解决问题的方法。
The eval
function takes a string and then returns the value of that string considered as a math operation. For example,
eval函数接受一个字符串,然后返回被视为数学运算的字符串的值。例如,
eval("3 + 4")
will return 7 as 3 + 4 = 7.
将返回7为3 + 4 = 7。
This helps in your case because you have an array of strings that you want you want to treat as the mathematical operators and operands that they represent.
这有助于您的情况,因为您有一个字符串数组,您希望将它们视为它们所代表的数学运算符和操作数。
myArray = ["225", "+", "15", "-", "10"];
Without converting any of the strings in your array into integers or operators you could write
不将数组中的任何字符串转换为可以编写的整数或运算符
eval(myArray[0] + myArray[1] + myArray[2]);
which will translate to
这将转化为
eval("225" + "+" + "15");
which will translate to
这将转化为
eval("225 + 15");
which will return 240, the value of adding 225 to 15.
这将返回240,将值225添加到15。
Or even more generic:
甚至更通用:
eval(myArray.join(' '));
which will translate to
这将转化为
eval("225 + 15 - 10");
which will return 230, the value of adding 15 to 225 and substract 10. So you can see that in your case it may be possible to skip converting the strings in your array into integers and operators and, as Oliver Morgan said, just eval
it.
这将返回230,增加15到225和减去10的值。所以你可以看到,在你的情况下,可以跳过将数组中的字符串转换为整数和运算符,正如Oliver Morgan所说,只是评估它。
#3
0
Consider using isNaN(Number(array[x]))
and a switch
for anything that doesn't satisfy the condition.
考虑使用isNaN(Number(array [x]))和一个不满足条件的开关。