Array.prototype.lastIndexOf()

  • Revision slug: JavaScript/Reference/Global_Objects/Array/lastIndexOf
  • Revision title: Array lastIndexOf method
  • Revision id: 29155
  • Created:
  • Creator: PointedEars
  • Is current revision? No
  • Comment /* Summary */ consistent JavaScript version

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 (Gecko 1.8b2 and later)
ECMAScript Edition: none

Syntax

lastIndexOf(searchElement{{mediawiki.external(', <i>fromIndex</i>')}})

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).

Examples

Example: Using lastIndexOf

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

array = [2, 5, 9, 2];
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.

indices = [];
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 regardsless the fromIndex parameter if it is the first element of the array. This is different from the indexOf method.

See also

Core JavaScript 1.5 Reference:Global Objects:Array:indexOf

Revision Source

<p>
</p>
<h3 name="Summary"> Summary </h3>
<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="fullwidth-table">
<tbody><tr>
<td class="header" colspan="2">Method of <a href="en/Core_JavaScript_1.5_Reference/Objects/Array">Array</a></td>
</tr>
<tr>
<td>Implemented in:</td>
<td>JavaScript 1.6 (Gecko 1.8b2 and later)</td>
</tr>
<tr>
<td>ECMAScript Edition:</td>
<td>none</td>
</tr>
</tbody></table>
<h3 name="Syntax"> Syntax </h3>
<p><code>
lastIndexOf(<i>searchElement</i>{{mediawiki.external(', &lt;i&gt;fromIndex&lt;/i&gt;')}})<br>
</code>
</p>
<h3 name="Parameters"> Parameters </h3>
<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>
<h3 name="Description"> Description </h3>
<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>
<h3 name="Examples"> Examples </h3>
<h4 name="Example:_Using_lastIndexOf"> Example: Using lastIndexOf </h4>
<p>The following example uses <code>lastIndexOf</code> to locate values in an array.
</p>
<pre>array = [2, 5, 9, 2];
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>
<h4 name="Example:_Finding_all_the_occurrences_of_an_element"> Example: Finding all the occurrences of an element </h4>
<p>The following example uses <code>lastIndexOf</code> to find all the indices of an element in a given array, using <a href="en/Core_JavaScript_1.5_Reference/Objects/Array/push">push</a> to add them to another array as they are found.
</p>
<pre>indices = [];
idx = array.lastIndexOf(element)
while (idx != -1) {
   indices.push(idx);
   idx = (idx &gt; 0 ? array.lastIndexOf(element, idx - 1) : -1);
}
</pre>
<p>Note that we have to handle the case <code>idx == 0</code> separately here because the element will always be found regardsless the <code>fromIndex</code> parameter if it is the first element of the array. This is different from the <a href="en/Core_JavaScript_1.5_Reference/Objects/Array/indexOf">indexOf</a> method.
</p>
<h3 name="See_also"> See also </h3>
<p><a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Array/indexOf">Core JavaScript 1.5 Reference:Global Objects:Array:indexOf</a>
</p>
Revert to this revision