aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/array/lastindexof/index.html
diff options
context:
space:
mode:
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/array/lastindexof/index.html')
-rw-r--r--files/zh-cn/web/javascript/reference/global_objects/array/lastindexof/index.html176
1 files changed, 176 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/array/lastindexof/index.html b/files/zh-cn/web/javascript/reference/global_objects/array/lastindexof/index.html
new file mode 100644
index 0000000000..51cf89a71f
--- /dev/null
+++ b/files/zh-cn/web/javascript/reference/global_objects/array/lastindexof/index.html
@@ -0,0 +1,176 @@
+---
+title: Array.prototype.lastIndexOf()
+slug: Web/JavaScript/Reference/Global_Objects/Array/lastIndexOf
+translation_of: Web/JavaScript/Reference/Global_Objects/Array/lastIndexOf
+---
+<div>{{JSRef("Global_Objects", "Array")}}</div>
+
+<h2 id="Summary" name="Summary">概述</h2>
+
+<p><code><strong>lastIndexOf()</strong></code> 方法返回指定元素(也即有效的 JavaScript 值或变量)在数组中的最后一个的索引,如果不存在则返回 -1。从数组的后面向前查找,从 <code>fromIndex</code> 处开始。</p>
+
+<div>{{EmbedInteractiveExample("pages/js/array-lastindexof.html")}}</div>
+
+<h2 id="Syntax" name="Syntax">语法</h2>
+
+<pre><var>arr</var>.lastIndexOf(<var>searchElement[</var>, <var>fromIndex]</var>)</pre>
+
+<h3 id="Parameters" name="Parameters">参数</h3>
+
+<dl>
+ <dt><code>searchElement</code></dt>
+ <dd>被查找的元素。</dd>
+ <dt><code>fromIndex</code> {{optional_inline}}</dt>
+ <dd>从此位置开始逆向查找。默认为数组的长度减 1(<code>arr.length - 1</code>),即整个数组都被查找。如果该值大于或等于数组的长度,则整个数组会被查找。如果为负值,将其视为从数组末尾向前的偏移。即使该值为负,数组仍然会被从后向前查找。如果该值为负时,其绝对值大于数组长度,则方法返回 -1,即数组不会被查找。</dd>
+ <dd>
+ <h3 id="返回值">返回值</h3>
+
+ <p>数组中该元素最后一次出现的索引,如未找到返回-1。</p>
+ </dd>
+</dl>
+
+<h2 id="Description" name="Description">描述</h2>
+
+<p><code>lastIndexOf</code> 使用<a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Comparison_Operators#Using_the_Equality_Operators">严格相等</a>(strict equality,即 ===)比较 <code>searchElement</code> 和数组中的元素。</p>
+
+<h2 id="Examples" name="Examples">示例</h2>
+
+<h3 id="Example_Using_lastIndexOf" name="Example:_Using_lastIndexOf">例子:使用 lastIndexOf</h3>
+
+<p>下例使用 <code>lastIndexOf</code> 定位数组中的值。</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">例子:查找所有元素</h3>
+
+<p>下例使用 <code>lastIndexOf</code> 查找到一个元素在数组中所有的索引(下标),并使用 {{jsxref("Array.push", "push")}} 将所有添加到另一个数组中。</p>
+
+<pre class="brush: js">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 &gt; 0 ? array.lastIndexOf(element, idx - 1) : -1);
+}
+
+console.log(indices);
+// [4, 2, 0];
+
+</pre>
+
+<p>注意,我们要单独处理<code>idx==0</code>时的情况,因为如果是第一个元素,忽略了<code>fromIndex</code>参数则第一个元素总会被查找。这不同于{{jsxref("Array.prototype.indexOf", "indexOf")}}方法</p>
+
+<p>注:原英文是针对使用三元操作符语句的作用进行说明的:<br>
+ <code>idx = (idx &gt; 0 ? array.lastIndexOf(element, idx - 1) : -1);</code><br>
+ <code>idx &gt; 0</code>时,才进入lastIndexOf由后往前移一位进行倒查找;如果<code>idx == 0</code>则直接设置<code>idx = -1</code>,循环<code>while (idx != -1)</code>就结束了。<br>
+  </p>
+
+<h2 id="Compatibility" name="Compatibility">兼容旧环境(Polyfill)</h2>
+
+<p><code>lastIndexOf</code> 在 ECMA-262 标准第 5 版被添加。因此它在不兼容该标准的浏览器中可能不被支持。你可以把下面代码添加到脚本中来使那些没有实现该方法的实现环境支持该方法。该算法是被 ECMA-262 第 5 版指定的。假定 {{jsxref("Global_Objects/Object", "Object")}}、{{jsxref("Global_Objects/TypeError", "TypeError")}}、{{jsxref("Global_Objects/Number", "Number")}}、{{jsxref("Math.floor")}}、{{jsxref("Math.abs")}},以及 {{jsxref("Math.min")}} 拥有其初始值。</p>
+
+<pre class="brush: js">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 &gt;&gt;&gt; 0;
+    if (len === 0) {
+      return -1;
+    }
+
+    n = len - 1;
+    if (arguments.length &gt; 1) {
+      n = Number(arguments[1]);
+      if (n != n) {
+        n = 0;
+      }
+      else if (n != 0 &amp;&amp; n != (1 / 0) &amp;&amp; n != -(1 / 0)) {
+        n = (n &gt; 0 || -1) * Math.floor(Math.abs(n));
+      }
+    }
+
+    for (k = n &gt;= 0
+          ? Math.min(n, len - 1)
+          : len - Math.abs(n); k &gt;= 0; k--) {
+      if (k in t &amp;&amp; t[k] === searchElement) {
+        return k;
+      }
+    }
+    return -1;
+  };
+}
+
+</pre>
+
+<p>另外,该实现是为了绝对兼容 Firefox 和 the SpiderMonkey JavaScript 引擎中的 <code>lastIndexOf</code>,包括了几种临界情况。如果你要在实际应用中使用该实现,可以忽略这些临界情况,从而简化 <code>fromIndex</code> 的计算。</p>
+
+<h2 id="Specifications">Specifications</h2>
+
+<table>
+ <tbody>
+ <tr>
+ <th scope="col">Specification</th>
+ <th scope="col">Status</th>
+ <th scope="col">Comment</th>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES5.1', '#sec-15.4.4.15', 'Array.prototype.lastIndexOf')}}</td>
+ <td>{{Spec2('ES5.1')}}</td>
+ <td>Initial definition. Implemented in JavaScript 1.6.</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES6', '#sec-array.prototype.lastindexof', 'Array.prototype.lastIndexOf')}}</td>
+ <td>{{Spec2('ES6')}}</td>
+ <td></td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ESDraft', '#sec-array.prototype.lastindexof', 'Array.prototype.lastIndexOf')}}</td>
+ <td>{{Spec2('ESDraft')}}</td>
+ <td></td>
+ </tr>
+ </tbody>
+</table>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <td></td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容性">浏览器兼容性</h2>
+
+<div>
+
+
+<p>{{Compat("javascript.builtins.Array.lastIndexOf")}}</p>
+</div>
+
+<h2 id="See_also" name="See_also">相关链接</h2>
+
+<ul>
+ <li>{{jsxref("Array.prototype.indexOf()")}}</li>
+ <li>{{jsxref("TypedArray.prototype.lastIndexOf()")}}</li>
+</ul>