Nodejs Array Last Index lastIndexOf(elt /*, from*/)

Here you can find the source of lastIndexOf(elt /*, from*/)

Method Source Code

/**/*from w w  w  .  j  av  a2  s  .  co m*/
 * Compares elements in the array with `searchElement` using strict equality
 * (===).  If any element matches `searchElement` the highest matching index is
 * returned.  Otherwise -1 is returned.
 *
 * You can optionally restrict the search by passing a `fromIndex` argument.
 *
 * This definition is compatible with the JavaScript 1.6 definition for
 * `Array#indexOf` in Spidermonkey.
 *
 * This implementation comes from:
 * https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/indexOf
 *
 * @function
 * @param   {any}       searchElement   element to search for within the array
 * @param   {number}    [fromIndex]     index at which to begin search
 * @returns {number}    the index of the matching element if one is found, -1 otherwise
 */
Array.prototype.lastIndexOf = Array.prototype.lastIndexOf || function(elt /*, from*/)  {
    var len = this.length;

    var from = Number(arguments[1]);
    if (isNaN(from)) {
        from = len - 1;
    } else {
        from = (from < 0)
            ? Math.ceil(from)
            : Math.floor(from);
        if (from < 0) {
            from += len;
        } else if (from >= len) {
            from = len - 1;
        }
    }

for (; from > -1; from--) {
    if (from in this &&
        this[from] === elt)
        return from;
    }
    return -1;
};

Related

  1. lastIndex(searchElement)
    Array.prototype.lastIndexOf = function lastIndexOf(searchElement) {
      if (!(this instanceof Object)) {
        throw new TypeError(this + 'is not an object');
      var
      array = this,
      arrayIsString = array instanceof String,
      length = array.length,
      index = 1 in arguments ? Number(arguments[1]) || 0 : length - 1;
    ...
    
  2. lastIndexOf(e)
    Array.prototype.lastIndexOf = function (e) {
        for (var i = this.length - 1, j; j = this[i]; i--) {
            if (j == e) { return i; }
        return -1;
    
  3. lastIndexOf(elem, from)
    Array.prototype.lastIndexOf = function(elem, from) {
      if (from == null) {
        from = this.length - 1;
      } else if (from < 0) {
        from = Math.max(0, this.length + from);
      for (var i = from; i >= 0; i--) {
        if (this[i] === elem) {
          return i;
    ...
    
  4. lastIndexOf(item)
    Array.prototype.lastIndexOf = function(item){
      var lastIndex = -1;
      for (var i=0; i<this.length; i++){
        if (this[i] === item){
          lastIndex = i;
      return lastIndex;
    
  5. lastIndexOf(o)
    Array.prototype.lastIndexOf = function (o) {
        var index = this.length;
        for (; index >= 0; index--) {
            if (index in this && this[index] === o)
                return index;
        return -1;
    };
    
  6. lastIndexOf(searchElement, fromIndex)
    Array.prototype.lastIndexOf = function(searchElement, fromIndex) {
      if (!fromIndex || !isFinite(fromIndex)) {
        fromIndex = this.length - 1;
      for (var i = fromIndex; i >= 0; --i) {
        if (this.hasOwnProperty(i) && this[i] === searchElement) {
          return i;
      return -1;
    
  7. lastIndexOf(v,b)
    Array.prototype.lastIndexOf = function(v,b){
        var idx=-1;
      if(b===true && typeof(v)=="function"){
        for (var i=this.length-1;i>=0;i--) {
          if(v(this[i])){idx=i; break;}
      } else {
        for (var i=this.length-1;i>=0;i--) {
          if(this[i]===v){idx=i; break;}
    ...