Nodejs Array First or Default firstOrDefault(expr)

Here you can find the source of firstOrDefault(expr)

Method Source Code

"use strict";//from   w w  w. j  a va 2s  . c om
var firstOrDefault = function (array, expr) {
    if (typeof expr === "function") {
        array = array.filter(expr);
    }
    return typeof array[0] === "undefined" ? null : array[0];
};
Array.prototype.firstOrDefault = function (expr) {
    return firstOrDefault(this, expr);
};
module.exports = firstOrDefault;

Related

  1. firstOrDefault(func)
    Array.prototype.firstOrDefault = function(func) {
      var elements = this.filter(func)
      if (elements.length > 0)
        return elements[0]
      else
        return undefined
    
  2. firstOrDefault(predicate)
    Array.prototype.firstOrDefault = function (predicate) {
        if (predicate && typeof predicate === 'function') {
            for (var i = 0; i < this.length; i++) {
                if (predicate(this[i])) return this[i];
            return null;
        } else {
            return this.length > 0 ? this[0] : null;
    };