Nodejs Array Last last(predicate, def)

Here you can find the source of last(predicate, def)

Method Source Code

Array.prototype.last = function (predicate, def) {
   var l = this.length;
   if (!predicate) return l ? this[l - 1] : def == null ? null : def;
   while (l-- > 0){
      if (predicate(this[l], l, this))
         return this[l];
   }//from w w w  . ja  v a  2 s . c o  m

   return def == null ? null : def;
};

Related

  1. last(fn, scope)
    Array.prototype.last = function(fn, scope) {
        if (Object.prototype.toString.call(this) !== '[object Array]') {
            throw new TypeError("`this` must be Array, not " + typeof this);
        if (typeof fn === 'undefined') {
            return this[this.length - 1];
        if (typeof fn !== 'function') {
            throw new TypeError("Optional argument[0] must be predicate function if defined");
    ...
    
  2. last(func)
    Array.prototype.last = function (func) {
        var length = this.length - 1,
            item = null;
        for (var i = length; i >= 0; i--) {
            if (func(this[i])) {
                item = this[i];
                break;
        return item;
    
  3. last(n)
    Array.prototype.last = function (n) {
        'use strict';
        return this.slice(this.length - (n || 1), this.length);
    };
    
  4. last(n)
    Array.prototype.last = function(n) {
      if (this.length == 0 || n < 0) return void 0;
      if (n == undefined) return this[this.length - 1];
      if (n == 0) return [];
      return this.rest(this.length - n);
    };
    
  5. last(num)
    Array.prototype.last = function (num) {
       if (num !== undefined) {
          return this.slice(-num);
       return this[this.length - 1];
    };
    
  6. last(t)
    Array.prototype.last = function(t) {
      if (t !== undefined) {
        this[this.length - 1] = t;
      } else {
        return this[this.length - 1];
    
  7. last(value)
    Array.prototype.last = function(value) {
      if (this.length) {
        var last = this[this.length-1];
        if (value != undefined)
          return (value == last) ? true : false;
        else  
          return last;
      return '';
    ...
    
  8. last(value)
    Array.prototype.last = function(value) {
      if (this.length) {
        var last = this[this.length-1];
        if (value != undefined)
          return (value == last) ? true : false;
        else  
          return last;
      return '';
    ...
    
  9. last100()
    Array.prototype.last100 = function () {
        if (this.length < 100) return this;
        var last100 = [];
        var start = this.length-101;
        for (var i = 0; i < 100; i++)
          last100.push(this[i + start]);
        return last100;