Array lastIndexOf()

JavaScript Array lastIndexOf() Definition

The LastIndexOf() method searches the specified item in the array. The search starts from the specified location. If no value is entered, it searches from end to end. Returns the value when it finds the value. If there are multiple values, it returns the last scan.

Returns -1 if the element is not in the array.

Note: Use the indexOf () method to search from start to finish.

Syntax:

array.lastIndexOf(item, start)

item( required ): The item to be searched is enter.
start ( optional ): Negative values will start at the given position counting from the end, and search to the beginning.


Let’s understand the logic with a small example.

        let arr = [1, 2, 3, 4, 5];
        console.log(arr.lastIndexOf(2))

1

It scans the list named “arr” from the last to the first element. Returns the value at the time it was found.

If there is more than one in the same element:

        let list = [1, 2, 3, 2, 2];
        console.log(list.lastIndexOf(2))

4

As seen, it returns the last element.


JavaScript Array lastIndexOf() Examples

Example 1

        let names = ["alice", "roo", "roy", "oliver"];
        console.log(names.lastIndexOf("alice"));
        console.log(names.lastIndexOf("roo"));
        console.log(names.lastIndexOf("roy"));
        console.log(names.lastIndexOf("oliver"));
        console.log(names.lastIndexOf("rick"));
        console.log(names.lastIndexOf("john"));

output:

0
1
2
3
-1
-1


Example 2

        let users = [
            { password: "john" },
            { password: "rick" },
            { password: "alice" },
            { password: "roy" }
        ]

        let arr = users.map(el => el.password).lastIndexOf("alice");
        console.log(arr)

output:

2


Example 3

        let list = ["zero", "first", "second", "third"];
        console.log(list.lastIndexOf("second"));
        console.log(list.lastIndexOf("second",1));

output:

2
-1


Polyfill:

if (!Array.prototype.lastIndexOf) {
  Array.prototype.lastIndexOf = function(searchElement /*, fromIndex*/) {
    'use strict';

    if (this === void 0 || this === null) {
      throw new TypeError();
    }

    var n, k,
      t = Object(this),
      len = t.length >>> 0;
    if (len === 0) {
      return -1;
    }

    n = len - 1;
    if (arguments.length > 1) {
      n = Number(arguments[1]);
      if (n != n) {
        n = 0;
      }
      else if (n != 0 && n != (1 / 0) && n != -(1 / 0)) {
        n = (n > 0 || -1) * Math.floor(Math.abs(n));
      }
    }

    for (k = n >= 0 ? Math.min(n, len - 1) : len - Math.abs(n); k >= 0; k--) {
      if (k in t && t[k] === searchElement) {
        return k;
      }
    }
    return -1;
  };
}

Browser Support

Chromeyes
Edgeyes
Firefox1.5
Internet Explorer9
Operayes
Safariyes
Android webviewyes
Chrome for Androidyes
Edge mobileyes
Firefox for Android4
Opera Androidyes

2 thoughts on “Array lastIndexOf()

Add yours

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Create a website or blog at WordPress.com

Up ↑

Design a site like this with WordPress.com
Get started