Nodejs Array Last Index lastIndex(searchElement)

Here you can find the source of lastIndex(searchElement)

Method Source Code

Array.prototype.lastIndexOf = function lastIndexOf(searchElement) {
   if (!(this instanceof Object)) {
      throw new TypeError(this + 'is not an object');
   }//from  w  ww  .ja v a 2s. co m

   var
   array = this,
   arrayIsString = array instanceof String,
   length = array.length,
   index = 1 in arguments ? Number(arguments[1]) || 0 : length - 1;

   index = index >= 0 ? index : Math.max(length + index, 0);

   for (; index >= 0; --index) {
      if ((
         arrayIsString && array.charAt(index) === searchElement
      ) || (
         index in array && array[index] === searchElement
      )) {
         return index;
      }
   }

   return -1;
};

Related

  1. 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;
    
  2. 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;
    ...
    
  3. lastIndexOf(elt /*, from*/)
    Array.prototype.lastIndexOf = Array.prototype.lastIndexOf || function(elt )  {
        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);
    ...
    
  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;