It seems like you are searching for a versatile function that can be passed any other function. If that's the case, it may be a bit more complex, but not excessively so.
// Maybe 'opposite' would be a better name as 'reverse' already exists
const opposite = f => ((...args) => !f(...args));
const predicate = e => e > 3;
const arr = [1, 2, 3, 4, 5];
console.log(arr.filter(predicate));
console.log(arr.filter(opposite(predicate)));
Essentially, opposite
is a function that produces another function, and the return value of that produced function is the boolean opposite of the original function's return value.
The ...
symbolizes the Spread syntax, which, in this context, instructs JavaScript to accept an unspecified number of arguments and pass them through as the same number of parameters.
However, if you are not seeking a general solution, a simpler approach would be to negate the function's return value directly within the .filter
method:
const predicate = e => e > 3;
const arr = [1, 2, 3, 4, 5];
console.log(arr.filter(predicate));
console.log(arr.filter((...args) => !predicate(...args)));