Skip to content Skip to sidebar Skip to footer

Arrays Of Functions To One Function(logical Conjunction)

I have a array of functions which each of them returns boolean value: (data, filter) => boolean (depending on whether the parameter meets the condition stored in the function) a

Solution 1:

Given this kind of array of functions:

const expressions = [
    (data, filter) =>true,
    (data, filter) =>false,
    ...
];

You can evaluate them all like this (d and f are assumed to be the values to pass to each function):

expressions.every(exp =>exp(d, f))
// or
expressions.reduce((res, exp) => res && exp(d, f), true)

Array#every will stop as soon as any function returns false. You can use Array#some to test the opposite, whether any function returns true. Array#reduce loops through all expressions instead and is closer to what you were attempting; in this particular example it's not very useful, but can be helpful if you need to combine your return values in other ways.

Post a Comment for "Arrays Of Functions To One Function(logical Conjunction)"