Core JavaScript 1.5 Reference:Global Objects:Array:lastIndexOf
From MDC
Contents |
[edit] 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 |
[edit] Syntax
var index = array.lastIndexOf(searchElement[, fromIndex]);
[edit] 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.
[edit] Description
lastIndexOf compares searchElement to elements of the Array using strict equality (the same method used by the ===, or triple-equals, operator).
[edit] Compatibility
lastIndexOf is a JavaScript extension 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 ECMA-262 implementations which do not natively support it. This algorithm is exactly the one used in Firefox and SpiderMonkey.
if (!Array.prototype.lastIndexOf)
{
Array.prototype.lastIndexOf = function(elt /*, from*/)
{
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);
if (from < 0)
from += len;
else if (from >= len)
from = len - 1;
}
for (; from > -1; from--)
{
if (from in this &&
this[from] === elt)
return from;
}
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.
[edit] Examples
[edit] 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
[edit] 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.