Array.prototype.lastIndexOf()
Baseline
Widely available
This feature is well established and works across many devices and browser versions. Itâs been available across browsers since 2015å¹´7æ.
lastIndexOf() æ¹æ³æåå³çµ¦å®å
ç´ æ¼é£å䏿å¾ä¸å被æ¾å°ä¹ç´¢å¼ï¼è¥ä¸å卿¼é£åä¸ååå³ -1ãæå°çæ¹åçºç±é£åå°¾é¨åå¾ï¼å³ååï¼å°æ¾ï¼åå§æ¼ fromIndexã
å試ä¸ä¸
const animals = ["Dodo", "Tiger", "Penguin", "Dodo"];
console.log(animals.lastIndexOf("Dodo"));
// Expected output: 3
console.log(animals.lastIndexOf("Tiger"));
// Expected output: 1
èªæ³
arr.lastIndexOf(searchElement) arr.lastIndexOf(searchElement, fromIndex)
忏
searchElement-
欲å¨é£å䏿å°çå ç´ ã
fromIndex鏿æ§-
è¦ç±é£åå°¾é¨åå¾ï¼å³ååï¼æå°çåå§ç´¢å¼ãé è¨çºé£åé·åº¦æ¸ä¸ï¼
arr.length - 1ï¼ï¼å³ææå°æ´åé£åãåå¦ç´¢å¼å¤§æ¼çæ¼é£åé·åº¦ï¼ææå°æ´åé£åãå¦æç´¢å¼å¼çºè² æ¸ï¼æå¾é£åçæå¾ä¸åå¾åç®ï¼æå¾ä¸åçç´¢å¼å¼çº -1ï¼ä»¥æ¤é¡æ¨ã注æï¼å管å¾åç®ï¼ä½ä¾ç¶æå¾å³å¾å·¦å ¨é¨æå°ãå¦æè² æ¸ç´¢å¼å¼å¨åé è¨ç®ä¹å¾ä»ç¶å°æ¼ 0ï¼å°æåå³ -1ï¼å³ä¸ææå°é£åã
åå³å¼
å¨é£å䏿¾å°çæå¾ä¸åå ç´ ç´¢å¼å¼ï¼æ²æ¾å°åçº -1ã
æè¿°
lastIndexOf compares searchElement to elements of the Array using strict equality (the same method used by the ===, or triple-equals, operator).
ç¯ä¾
>ä½¿ç¨ lastIndexOf
The following example uses lastIndexOf to locate values in an array.
var numbers = [2, 5, 9, 2];
numbers.lastIndexOf(2); // 3
numbers.lastIndexOf(7); // -1
numbers.lastIndexOf(2, 3); // 3
numbers.lastIndexOf(2, 2); // 0
numbers.lastIndexOf(2, -2); // 0
numbers.lastIndexOf(2, -1); // 3
å°æ¾è©²å ç´ ææåºç¾å¨é£åä¸çä½ç½®
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 array = ["a", "b", "a", "c", "a", "d"];
var element = "a";
var idx = array.lastIndexOf(element);
while (idx != -1) {
indices.push(idx);
idx = idx > 0 ? array.lastIndexOf(element, idx - 1) : -1;
}
console.log(indices);
// [4, 2, 0]
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.
Polyfill
lastIndexOf was added to the ECMA-262 standard in the 5th edition; 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 Object, TypeError, Number, Math.floor, Math.abs, and Math.min have their original values.
// Production steps of ECMA-262, Edition 5, 15.4.4.15
// Reference: http://es5.github.io/#x15.4.4.15
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;
};
}
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.
è¦ç¯
| Specification |
|---|
| ECMAScript® 2027 Language Specification> # sec-array.prototype.lastindexof> |
ç覽å¨ç¸å®¹æ§
ç¸å®¹æ§å註
- Starting with Firefox 47, this method will no longer return
-0. For example,[0].lastIndexOf(0, -0)will now always return+0(Firefox bug 1242043).