2

I have dynamic condition for if, how can I check it by using as this:-

var searchQuery = '(data.id == "1") && (data.first == "et")';
if(searchQuery === true)
    alert('yes');

this is only checking that variable is empty or not. but I need to check variable's value as statement and check it by if condition.

variable value is not static , even in value more than 2 arguments may exist. how to resolve this?

Zee
  • 8,420
  • 5
  • 36
  • 58
Ankit
  • 562
  • 5
  • 12

2 Answers2

4

You could use the evil eval():

var searchQuery = '(data.id == "1") && (data.first == "et")';

if(eval(searchQuery))
    alert('yes');

It evaluates a string as JavaScript code.

Scimonster
  • 32,893
  • 9
  • 77
  • 89
0

You can use eval

var searchQuery = '(data.id == "1") && (data.first == "et")';

if(eval(searchQuery) === true)
    alert('yes');

or Function constructor:

if (new Function('return ' + searchQuery)() === true)
    alert('yes');
jcubic
  • 61,973
  • 54
  • 229
  • 402