2

I want to make a dynamic filter method, but don't know how to include conditions dynamically.

Example

var do_test_1 = true,
    do_test_2 = false,
    do_test_3 = true;

Now there are three tests:

if (foo == bar)  // test 1
if (john == doe) // test 2
if (jane == doe) // test 3

Now I want to build a dynamic if clause, based on the do_test vars.

Real use case

I have a list of tasks and want to filter them:

  • If the "assigned to me" filter is active, it should return only the the tasks that are assigned to me.
  • If the "high priority" filter and the "assigned to me" filter is active, it should return only the tasks that are assigned to me and have a high priority.
  • ... and so on ...

I played around with the filter method, but I only get it working with an OR logic (show tasks that are assigned to me or have a high priority):

var $show = $tasks.filter(function(index, task) {
  var $task = $(task);
  return ((task.data('assigned') && filters['mytasks']) || (task.data('priority') == 3 && filters['priority']))
});
0

1 Answer 1

3

use eval() function

for(var i=0;i<3;i++){
    var f=i+1;
    eval("if(f==i+1){alert(i);}");   
}

Demo: http://jsfiddle.net/ke42b/2/

This is just a tip on how to create dynamic if conditions. Now you know how to create dynamic if conditions.

Code accordingly to your needs !

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

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.