Dynamic generated condition JS

here is a simple one:

OR dynamic condition

const conditions = [false, 0, 'blah', 1]; // whatever conditions you like

conditions.reduce((previousCond, actualCond) =>  
previousCond || actualCond, false)); // => 'blah'

I like this one because it behave as a real condition and return the real value, not just a boolean.


If you want to return a boolean, you could have used:

conditions.some(cond => cond); // => true

If you want an and condition user Array.every instead of .some


I'd like to suggest an alternative solution without string manipulation at all. Instead you can combine functions. Building up code through function composition rather than string manipulation should be less error-prone - and surely less 'hacky' (whatever that means exactly...).

Javascript has first-order functions, you can just pass around (and combine) predicates. A predicate is a function which returns a boolean. For example:

var isLabelTest = function(obj) {
  return obj.label === 'test';
}

Rather than storing conditions as strings, you can store them as predicate functions.
Then you can write some functions which take predicates and return new predicates:

var binaryAnd = function(predA, predB) {
  return function(obj) {
    return predA(obj) && predB(obj);
  };
};

var binaryOr = function(predA, predB) {
  return function(obj) {
    return predA(obj) || predB(obj);
  };
};

You could also write functions which take an array of predicates and combine all predicates in the array into a new predicate:

var and = function(preds) {
  return preds.reduce(binaryAnd, function(obj) {return true;});
};

var or = function(preds) {
  return preds.reduce(binaryOr, function(obj) {return false;});
};

Now given an "andArray" of predicates which all need to return true, and an "orArray" of predicates of which at least one needs to return true, the following code does the job:

var results = [];
var combinedPredicate = binaryAnd(and(andArray), or(orArray));
var pushIfCondition = function (obj) {
  results.push(combinedPredicate(obj));
};
dataArray.forEach(pushIfCondition);

Finally, note that it is not necessary to write those functions for combining predicates yourself, libraries like ramda can provide more efficient implementations.


Create a new Function(), this way the string will be parsed only once:

var length = dataArray.length;
var result = [];
var fn = new Function('data', 'return ' + whereCondition);

for (var i = 0; i < length; i++) {
    var data = dataArray[i];
    if (fn(data)) {
        result.push(obj);
    }
}

Here is a simple trick using filter

Example 1:

> const num = 10
> const rules = []
> rules.push(num > 5)  // true
> rules.push(num % 2 !== 0)  // to check if it is odd number w/c will be "false"
> console.log(rules)
< [true, false]

> rules.filter((rule) => rule === true).length === rules.length
< false  // Did not pass all the conditions

Example 2

> const num = 7
> const rules = []
> rules.push(num > 5)  // true
> rules.push(num % 2 !== 0)  // true
> console.log(rules)
< [true, true]

> rules.filter((rule) => rule === true).length === rules.length
< true  // Passed all the conditions

Tags:

Javascript