I'm running a lot of functions to build a mongoDb request. Based on their response (if not falsy
) I'm appending the return values to an array ruleConditions[]
which is used later.
Currently I'm doing it this way:
//building ruleConditions
var ruleConditions = {$and: []};
var fromConditions = mongoRequestsHelper.getFromConditions(rule.from);
if (fromConditions) ruleConditions.$and.push(fromConditions);
//and 4 similar blocks
Is there any way to avoid unneeded variables creation (carTypesConditions
...)?
Calling functions twice like:
if (mongoRequestsHelper.getFromConditions(rule.moversFilter)) ruleConditions.push(mongoRequestsHelper.getFromConditions(rule.moversFilter))
is not a good idea obviously.
if(getMoversConditions()) conditions.push(getMoversConditions())
. Depending on how long it takes to run your functions, this is a bad approach performance-wise, though, because it requires your to call each function twice. \$\endgroup\$false
, but for falsy. If a function returns either of0
,""
,null
orundefined
, your condition will also omit that push. I'm saying that because I altered your question before and changed that to falsy - which you changed back tofalse
. \$\endgroup\$