Array.prototype.lastIndexOf()

  • Revision slug: Web/JavaScript/Reference/Global_Objects/Array/lastIndexOf
  • Revision title: Array.lastIndexOf
  • Revision id: 454263
  • Created:
  • Creator: Brettz9
  • Is current revision? No
  • Comment

Revision Content

Summary

Returns the last index at which a given element can be found in the array, or -1 if it is not present. The array is searched backwards, starting at fromIndex.

Method of Array
Implemented in JavaScript 1.6
ECMAScript Edition ECMAScript 5th Edition

Syntax

array.lastIndexOf(searchElement[, fromIndex])

Parameters

searchElement
Element to locate in the array.
fromIndex
The index at which to start searching backwards. Defaults to the array's length, i.e. the whole array will be searched. If the index is greater than or equal to the length of the array, the whole array will be searched. If negative, it is taken as the offset from the end of the array. Note that even when the index is negative, the array is still searched from back to front. If the calculated index is less than 0, -1 is returned, i.e. the array will not be searched.

Description

lastIndexOf compares searchElement to elements of the Array using strict equality (the same method used by the ===, or triple-equals, operator).

Compatibility

lastIndexOf is a recent addition to the ECMA-262 standard; as such it may not be present in other implementations of the standard. You can work around this by inserting the following code at the beginning of your scripts, allowing use of lastIndexOf in implementations which do not natively support it. This algorithm is exactly the one specified in ECMA-262, 5th edition, assuming ObjectTypeErrorNumber, Math.floor, Math.abs, and Math.min have their original values.

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

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

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

    n = len;
    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;
  };
}

Again, note that this implementation aims for absolute compatibility with lastIndexOf in Firefox and the SpiderMonkey JavaScript engine, including in several cases which are arguably edge cases. If you intend to use this in real-world applications, you may be able to calculate from with less complicated code if you ignore those cases.

Examples

Example: Using lastIndexOf

The following example uses lastIndexOf to locate values in an array.

var array = [2, 5, 9, 2];
var index = array.lastIndexOf(2);
// index is 3
index = array.lastIndexOf(7);
// index is -1
index = array.lastIndexOf(2, 3);
// index is 3
index = array.lastIndexOf(2, 2);
// index is 0
index = array.lastIndexOf(2, -2);
// index is 0
index = array.lastIndexOf(2, -1);
// index is 3

Example: Finding all the occurrences of an element

The following example uses lastIndexOf to find all the indices of an element in a given array, using push to add them to another array as they are found.

var indices = [];
var idx = array.lastIndexOf(element);

while (idx != -1) {
  indices.push(idx);
  idx = (idx > 0 ? array.lastIndexOf(element, idx - 1) : -1);
}

Note that we have to handle the case idx == 0 separately here because the element will always be found regardless of the fromIndex parameter if it is the first element of the array. This is different from the indexOf method.

Browser compatibility

Based on Kangax's compat tables

{{CompatibilityTable}}
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} 9 {{CompatVersionUnknown}} {{CompatVersionUnknown}}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}

See also

Revision Source

<h2 id="Summary" name="Summary">Summary</h2>
<p>Returns the last index at which a given element can be found in the array, or -1 if it is not present. The array is searched backwards, starting at <code>fromIndex</code>.</p>
<table class="standard-table">
  <thead>
    <tr>
      <th class="header" colspan="2">Method of <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array" title="JavaScript/Reference/Global_Objects/Array"><code>Array</code></a></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Implemented in</td>
      <td>JavaScript 1.6</td>
    </tr>
    <tr>
      <td>ECMAScript Edition</td>
      <td>ECMAScript 5th Edition</td>
    </tr>
  </tbody>
</table>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
<code><em>array</em>.lastIndexOf(<em>searchElement</em>[, <em>fromIndex</em>])</code></pre>
<h2 id="Parameters" name="Parameters">Parameters</h2>
<dl>
  <dt>
    <code>searchElement</code></dt>
  <dd>
    Element to locate in the array.</dd>
  <dt>
    <code>fromIndex</code></dt>
  <dd>
    The index at which to start searching backwards. Defaults to the array's length, i.e. the whole array will be searched. If the index is greater than or equal to the length of the array, the whole array will be searched. If negative, it is taken as the offset from the end of the array. Note that even when the index is negative, the array is still searched from back to front. If the calculated index is less than 0, -1 is returned, i.e. the array will not be searched.</dd>
</dl>
<h2 id="Description" name="Description">Description</h2>
<p><code>lastIndexOf</code> compares <code>searchElement</code> to elements of the Array using strict equality (the same method used by the ===, or triple-equals, operator).</p>
<h2 id="Compatibility" name="Compatibility">Compatibility</h2>
<p><code>lastIndexOf</code> is a recent addition to the ECMA-262 standard; as such it may not be present in other implementations of the standard. You can work around this by inserting the following code at the beginning of your scripts, allowing use of <code>lastIndexOf</code> in implementations which do not natively support it. This algorithm is exactly the one specified in ECMA-262, 5th edition, assuming&nbsp;<code>Object</code>,&nbsp;<code>TypeError</code>,&nbsp;<code>Number</code>, <code>Math.floor</code>, <code>Math.abs</code>, and <code>Math.min</code> have their original values. <!-- var DO_NOT_MODIFY_THE_FOLLOWING_CODE_IN_ANY_WAY = 0; --></p>
<pre class="brush: js">
if (!Array.prototype.lastIndexOf) {
&nbsp; Array.prototype.lastIndexOf = function(searchElement /*, fromIndex*/) {
&nbsp;&nbsp;&nbsp; 'use strict';

&nbsp;&nbsp;&nbsp; if (this == null) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw new TypeError();
&nbsp;&nbsp;&nbsp; }

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

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

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

</pre>
<p>Again, note that this implementation aims for absolute compatibility with <code>lastIndexOf</code> in Firefox and the SpiderMonkey JavaScript engine, including in several cases which are arguably edge cases. If you intend to use this in real-world applications, you may be able to calculate <code>from</code> with less complicated code if you ignore those cases.</p>
<h2 id="Examples" name="Examples">Examples</h2>
<h3 id="Example:_Using_lastIndexOf" name="Example:_Using_lastIndexOf">Example: Using lastIndexOf</h3>
<p>The following example uses <code>lastIndexOf</code> to locate values in an array.</p>
<pre class="brush: js">
var array = [2, 5, 9, 2];
var index = array.lastIndexOf(2);
// index is 3
index = array.lastIndexOf(7);
// index is -1
index = array.lastIndexOf(2, 3);
// index is 3
index = array.lastIndexOf(2, 2);
// index is 0
index = array.lastIndexOf(2, -2);
// index is 0
index = array.lastIndexOf(2, -1);
// index is 3
</pre>
<h3 id="Example:_Finding_all_the_occurrences_of_an_element" name="Example:_Finding_all_the_occurrences_of_an_element">Example: Finding all the occurrences of an element</h3>
<p>The following example uses <code>lastIndexOf</code> to find all the indices of an element in a given array, using <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/push" title="JavaScript/Reference/Global_Objects/Array/push">push</a> to add them to another array as they are found.</p>
<pre class="brush: js">
var indices = [];
var idx = array.lastIndexOf(element);

while (idx&nbsp;!= -1) {
  indices.push(idx);
  idx = (idx &gt; 0&nbsp;? array.lastIndexOf(element, idx - 1)&nbsp;: -1);
}
</pre>
<p>Note that we have to handle the case <code>idx == 0</code> separately here because the element will always be found regardless of the <code>fromIndex</code> parameter if it is the first element of the array. This is different from the <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/indexOf" title="JavaScript/Reference/Global_Objects/Array/indexOf">indexOf</a> method.</p>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>Based on <a href="http://kangax.github.com/es5-compat-table/">Kangax's compat tables</a></p>
<div>
  {{CompatibilityTable}}</div>
<div id="compat-desktop">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Chrome</th>
        <th>Firefox (Gecko)</th>
        <th>Internet Explorer</th>
        <th>Opera</th>
        <th>Safari</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{CompatVersionUnknown}}</td>
        <td>{{CompatVersionUnknown}}</td>
        <td>9</td>
        <td>{{CompatVersionUnknown}}</td>
        <td>{{CompatVersionUnknown}}</td>
      </tr>
    </tbody>
  </table>
</div>
<div id="compat-mobile">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Android</th>
        <th>Chrome for Android</th>
        <th>Firefox Mobile (Gecko)</th>
        <th>IE Mobile</th>
        <th>Opera Mobile</th>
        <th>Safari Mobile</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{CompatUnknown}}</td>
        <td>{{CompatUnknown}}</td>
        <td>{{CompatUnknown}}</td>
        <td>{{CompatUnknown}}</td>
        <td>{{CompatUnknown}}</td>
        <td>{{CompatUnknown}}</td>
      </tr>
    </tbody>
  </table>
</div>
<h2 id="See_also" name="See_also">See also</h2>
<ul>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/indexOf" title="JavaScript/Reference/Global_Objects/Array/indexOf">indexOf</a></li>
</ul>
Revert to this revision