简体   繁体   English

如何使用 javascript 解决此问题? 我需要使用拆分吗?

[英]How can I solve this problem using javascript? do i need to use split?

I am learning JS.我正在学习JS。 suddenly I got the below problem from the inte.net and I become interested to solve this.突然我从 inte.net 得到了以下问题,我开始有兴趣解决这个问题。 But I am getting confused how can I do this?但是我很困惑我该怎么做? Do I need to use split?我需要使用拆分吗?

The problem is Using JS, convert:问题是使用 JS,转换:

[ "AND", ["<", "var1", "var2"], [ "OR", [">", "var3", "var4"], ["==", "var5", "var6"] ] } 
To: 
"var1 < val2 AND (var3 > val4 OR val5 == val6)"

Can anyone give me some idea?谁能给我一些想法? Thanks谢谢

I would first construct an object whose keys are the operator strings, and whose values are callbacks which, when called, produce the appropriate comparison result.我将首先构造一个 object,其键是运算符字符串,其值是回调,调用时会产生适当的比较结果。

To avoid eval and global variables, don't use separate variable names, but put them all into an object, so that you can use bracket notation to dynamically look up the appropriate property in the object.为避免eval和 global 变量,不要使用单独的变量名称,而是将它们全部放入 object 中,以便您可以使用括号表示法在 object 中动态查找相应的属性。

If either operator is an array, use recursion to evaluate those inner values first.如果任一运算符是数组,请先使用递归来评估这些内部值。 Otherwise, you can use the operator string to look up the function to call and call it.否则,您可以使用运算符字符串查找 function 调用并调用它。

 const constants = { var1: 1, var2: 2, var3: 3, var4: 4, var5: 5, var6: 6, }; const operations = { AND: (x, y) => x && y, OR: (x, y) => x || y, '<': (x, y) => x < y, '>': (x, y) => x > y, '==': (x, y) => x == y, }; const input = [ "AND", ["<", "var1", "var2"], [ "OR", [">", "var3", "var4"], ["==", "var5", "var6"] ] ]; const evaluate = ([operator, val1, val2]) => { val1 = (Array.isArray(val1))? evaluate(val1): constants[val1]; val2 = (Array.isArray(val2))? evaluate(val2): constants[val2]; return operations[operator](val1, val2); }; console.log(evaluate(input));

 const constants = { var1: 1, var2: 2, var3: 10, var4: 4, var5: 5, var6: 6, }; const operations = { AND: (x, y) => x && y, OR: (x, y) => x || y, '<': (x, y) => x < y, '>': (x, y) => x > y, '==': (x, y) => x == y, }; const input = [ "AND", ["<", "var1", "var2"], [ "OR", [">", "var3", "var4"], ["==", "var5", "var6"] ] ]; const evaluate = ([operator, val1, val2]) => { val1 = (Array.isArray(val1))? evaluate(val1): constants[val1]; val2 = (Array.isArray(val2))? evaluate(val2): constants[val2]; return operations[operator](val1, val2); }; console.log(evaluate(input));

If you want to convert some array to string maybe some recursive function works.如果你想将一些数组转换为字符串,也许一些递归 function 可以工作。

I am assuming that you have given an array where first element is condition and remaining 2 are the variables in which you want to put it.我假设您已经给出了一个数组,其中第一个元素是条件,剩下的 2 个是您想要放置的变量。

 var payload = [ 'AND', ['<', 'var1', 'var2'], ['OR', ['>', 'var3', 'var4'], ['==', 'var5', 'var6']], ] function convertFunction(array) { const condition = array[0]; let LHS = array[1]; let RHS = array[2]; if(Array.isArray(LHS)){ LHS = convertFunction(LHS); } if(Array.isArray(RHS)){ RHS = `(${convertFunction(RHS)})`; } return `${LHS} ${condition} ${RHS}` } console.log(convertFunction(payload));

Not sure if the correct algorithm is used to determine if brackets are needed.不确定是否使用正确的算法来确定是否需要括号。

 const input = [ "AND", ["<", "var1", "var2"], [ "OR", [">", "var3", "var4"], ["==", "var5", "var6"] ] ]; const strArr = (arr) => { const printSide = (side) => Array.isArray(side)? printBrackets(side): side; const printBrackets = (side) => Array.isArray(side[1]) && Array.isArray(side[2])? `(${strArr(side)})`: strArr(side); const [operator, left, right] = arr; return res = [printSide(left), operator, printSide(right)].join(' '); }; console.log(strArr(input));
 .as-console-wrapper{min-height: 100%;important: top: 0}

You may use "eval" in order to evaluate the values of the operators.您可以使用“eval”来评估运算符的值。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM