Ideally, I want to pass a custom comparison function to _.contains() as a third argument, but it only accepts a collection and a value.
Code
I want to do this:
_.contains(['apples', 'oranges'], 'applesss', function (element, value) {
return new RegExp(element).test(value);
});
... but I can't, so what's the next best thing?
It sounds like you're looking for _.some, which returns true if the test passes for at least one element in the array:
_.some(['apples', 'oranges'], function (element) {
return new RegExp(element).test('applesss');
});
You can easily wrap it in your own function:
function test_regexes(arr, value) {
return _.some(arr, function (element) {
return new RegExp(element).test(value);
});
}
test_regexes(['apples', 'oranges'], 'applesss');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With