Revision 29153 of Array.prototype.lastIndexOf()

  • Revision slug: JavaScript/Reference/Global_Objects/Array/lastIndexOf
  • Revision title: Array lastIndexOf method
  • Revision id: 29153
  • Created:
  • Creator: Maian
  • Is current revision? No
  • Comment Core JavaScript 1.5 Reference:Objects:Array:lastIndexOf moved to Core JavaScript 1.5 Reference:Global Objects:Array:lastIndexOf

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.5 (Gecko 1.8b2 and later)
ECMA Version: 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. If not provided, array length is used. If negative, it is taken as the offset from the end of the array (but no less than zero, even if the result of adding the offset and the array's length would be negative). If the index is greater than the length of the array, the length of the array will be used.

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.

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.5 (Gecko 1.8b2 and later)</td>
</tr>
<tr>
<td>ECMA Version:</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.  If not provided, array length is used.  If negative, it is taken as the offset from the end of the array (but no less than zero, even if the result of adding the offset and the array's length would be negative).  If the index is greater than the length of the array, the length of the array will be used.
</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>
Revert to this revision