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?

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.