In C#, we have Enumerable.First(predicate)
. Given this JavaScript code:
function process() {
var firstMatch = ['a', 'b', 'c'].filter(function(e) {
return applyConditions(e);
}).shift();
if(!firstMatch) {
return;
}
// do something else
}
function applyConditions(element) {
var min = 97;
var max = 122;
var random = Math.floor(Math.random() * (max - min + 1) + min);
return element === String.fromCharCode(random);
}
other than forEach
, using loop, using multiple or operators or implicitly calling some(predicate)
, is there a smarter way of finding the firstMatch
? Preferably a JavaScript function (something like filterFirst(pedicate)
) which short-circuits on first match resembling C#'s Enumerable.First()
implementation?
FWIW, I am targeting node.js / io.js runtimes.
SingleOrDefault() – Same as Single(), but it can handle the null value. First() - There is at least one result, an exception is thrown if no result is returned. FirstOrDefault() - Same as First(), but not thrown any exception or return null when there is no result.
The major difference between First and FirstOrDefault is that First() will throw an exception if there is no result data for the supplied criteria whereas FirstOrDefault() returns a default value (null) if there is no result data.
The default value for reference and nullable types is null . The FirstOrDefault method does not provide a way to specify a default value. If you want to specify a default value other than default(TSource) , use the DefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource) method as described in the Example section.
More Detail. Use the FirstorDefault() method to return the first element of a sequence or a default value if element isn't there.
No need to reinvent the wheel, the correct way to do it is to use .find
:
var firstMatch = ['a', 'b', 'c'].find(applyConditions);
If you're using a browser that does not support .find
you can polyfill it
You could emulate this in the case where you want to return the first truthy value with reduce
.
['a', 'b', 'c'].reduce(function(prev, curr) {
return prev || predicate(curr) && curr;
}, false);
edit: made more terse with @BenjaminGruenbaum suggestion
LINQ
users call first
and firstOrDefault
a lot with no predicate, which is not possible with find
. So,
first() {
var firstOrDefault = this.firstOrDefault();
if(firstOrDefault !== undefined)
return firstOrDefault;
else
throw new Error('No element satisfies the condition in predicate.');
}
firstOrDefault() {
return this.find(o => true);
}
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