Nodejs Array First Predicate first(f)

Here you can find the source of first(f)

Method Source Code

Array.prototype.first = function(f){
    var i = 0;//from ww w  .  j  av  a  2s  . c  o  m

    while(i < this.length){
        if(f == undefined){
            return this[i];
        }
        if(f(this[i])){
           return this[i];
        }
        i++;
    }
    return null;
};

Related

  1. first(fn)
    Array.prototype.first = function(fn) {
        if (Object.prototype.toString.call(this) !== '[object Array]') {
            throw new TypeError("`this` must be Array, not " + typeof this);
        if (typeof fn === 'undefined') {
            return this[0];
        if (typeof fn !== 'function') {
            throw new TypeError("Optional `argument[0]` must be predicate function if defined");
    ...
    
  2. first(func)
    Array.prototype.first = function(func) {
      return this.where(func)[0];
    };
    
  3. first(func)
    Array.prototype.first = function(func) {
        var result = [];
        for(var i=0;i<this.length;i++) 
            if (func(this[i]))
                return this[i];
        return null;
    
  4. first(func)
    Array.prototype.first = function (func) {
        var length = this.length,
            item = null;
        for (var i = 0; i < length; i++) {
            if (func(this[i])) {
                return this[i];
        return item;
    ...