Skip to main content
1-Visitor
June 18, 2020
Solved

Store a if/else expression inside a property or a table

  • June 18, 2020
  • 1 reply
  • 3145 views

Hi,

 

I have a quick query to be asked, I am creating a condition statement or a if/else condition dynamically and this statement is being stored in a property for testing purpose. I wanted to know how to use the expression stored inside the property for the if/else conditions because if I use the property directly it is evaluating if there is a value in the property and returning always true. But I want the condition inside the property to be evaluated.

Is there any solution for this purpose to use the dynamically created expressions as it is required?

 

Thanks in advance 

 

With Regards, 

Srijith

Best answer by rjanardan

I do not know if this fits your requirement or whether this is the best alternative. I am assuming left operand is always one of the properties of the thing. Please test properly. 

 

 

 

var exp = "me.prop < 10" ;

var operation = exp.split(" ");
var propValue = me[operation[0].split(".")[1]];

var result = false;
if(operation[1] == "<" && propValue < operation[2]){ // inlcude other operators using ||
 result = true;
}

 

 

1 reply

16-Pearl
June 18, 2020

eval() would have worked but its usage should be avoided. Never use eval() 

1-Visitor
June 22, 2020

I can use eval() as a solution but as it is mentioned to reduce the use of eval() is there any other way I can solve the same issue other than eval().

 

16-Pearl
June 22, 2020

If using eval() cannot be avoided, then ensure that the string being passed to it cannot be modified to contain any malicious code. That is, you should scrutinize the usergroups that could write to this property. Although tedious, you could check if, splitting the string into operands and operators and then looping over all operators, is an option.