--- title: Array.prototype.findIndex() slug: Web/JavaScript/Reference/Global_Objects/Array/findIndex tags: - Array - JavaScript - Method - Prototype translation_of: Web/JavaScript/Reference/Global_Objects/Array/findIndex --- <div>{{JSRef}}</div> <p><code><strong>findIndex()</strong></code>方法返回数组中满足提供的测试函数的第一个元素的<strong>索引</strong>。若没有找到对应元素则返回-1。</p> <div>{{EmbedInteractiveExample("pages/js/array-findindex.html")}}</div> <p>另请参见 {{jsxref("Array.find", "find()")}} 方法,它返回数组中找到的元素的<strong>值</strong>,而不是其索引。</p> <h2 id="Syntax" name="Syntax">语法</h2> <pre class="syntaxbox notranslate"><code><em>arr</em>.findIndex(<em>callback</em>[, <em>thisArg</em>])</code></pre> <h3 id="Parameters" name="Parameters">参数</h3> <dl> <dt><code>callback</code></dt> <dd>针对数组中的每个元素, 都会执行该回调函数, 执行时会自动传入下面三个参数: <dl> <dt><code>element</code></dt> <dd>当前元素。</dd> <dt><code>index</code></dt> <dd>当前元素的索引。</dd> <dt><code>array</code></dt> <dd>调用<code>findIndex</code>的数组。</dd> </dl> </dd> <dt><code>thisArg</code></dt> <dd>可选。执行<code>callback</code>时作为<code style="font-size: 14px; line-height: inherit;">this</code>对象<span style="line-height: inherit;">的值.</span></dd> </dl> <h3 id="返回值">返回值</h3> <p> 数组中通过提供测试函数的第一个元素的<strong>索引</strong>。否则,返回-1</p> <h2 id="Description" name="Description">描述</h2> <p><code>findIndex</code>方法对数组中的每个数组索引<code>0..length-1</code>(包括)执行一次<code>callback</code>函数,直到找到一个<code>callback</code>函数返回真实值(强制为<code>true</code>)的值。如果找到这样的元素,<code>findIndex</code>会立即返回该元素的索引。如果回调从不返回真值,或者数组的<code>length</code>为0,则<code>findIndex</code>返回-1。 与某些其他数组方法(如Array#some)不同,在稀疏数组中,即使对于数组中不存在的条目的索引也会调用回调函数。</p> <p>回调函数调用时有三个参数:元素的值,元素的索引,以及被遍历的数组。</p> <p>如果一个 <code>thisArg</code> 参数被提供给 <code>findIndex</code>, 它将会被当作<code>this</code>使用在每次回调函数被调用的时候。如果没有被提供,将会使用{{jsxref("undefined")}}。</p> <p><code>findIndex</code>不会修改所调用的数组。</p> <p>在第一次调用<code>callback</code>函数时会确定元素的索引范围,因此在<code>findIndex</code>方法开始执行之后添加到数组的新元素将不会被<code>callback</code>函数访问到。如果数组中一个尚未被<code>callback</code>函数访问到的元素的值被<code>callback</code>函数所改变,那么当<code>callback</code>函数访问到它时,它的值是将是根据它在数组中的索引所访问到的当前值。被删除的元素仍然会被访问到。</p> <h2 id="Examples" name="Examples">示例</h2> <h3 id="Example_Testing_size_of_all_array_elements" name="Example:_Testing_size_of_all_array_elements">查找数组中首个质数元素的索引</h3> <p><span style="line-height: inherit;">以下示例查找数组中素数的元素的索引(如果不存在素数,则返回-1)。</span></p> <pre class="brush: js notranslate">function isPrime(element, index, array) { var start = 2; while (start <= Math.sqrt(element)) { if (element % start++ < 1) { return false; } } return element > 1; } console.log([4, 6, 8, 12].findIndex(isPrime)); // -1, not found console.log([4, 6, 7, 12].findIndex(isPrime)); // 2</pre> <h2 id="Polyfill">Polyfill</h2> <pre class="brush: js notranslate">// https://tc39.github.io/ecma262/#sec-array.prototype.findIndex if (!Array.prototype.findIndex) { Object.defineProperty(Array.prototype, 'findIndex', { value: function(predicate) { // 1. Let O be ? ToObject(this value). if (this == null) { throw new TypeError('"this" is null or not defined'); } var o = Object(this); // 2. Let len be ? ToLength(? Get(O, "length")). var len = o.length >>> 0; // 3. If IsCallable(predicate) is false, throw a TypeError exception. if (typeof predicate !== 'function') { throw new TypeError('predicate must be a function'); } // 4. If thisArg was supplied, let T be thisArg; else let T be undefined. var thisArg = arguments[1]; // 5. Let k be 0. var k = 0; // 6. Repeat, while k < len while (k < len) { // a. Let Pk be ! ToString(k). // b. Let kValue be ? Get(O, Pk). // c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)). // d. If testResult is true, return k. var kValue = o[k]; if (predicate.call(thisArg, kValue, k, o)) { return k; } // e. Increase k by 1. k++; } // 7. Return -1. return -1; } }); } </pre> <p>如果您需要兼容不支持<code><a href="/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty">Object.defineProperty</a></code>的JavaScript引擎,那么最好不要对<code>Array.prototype</code>方法进行 polyfill ,因为您无法使其成为不可枚举的。</p> <p>使用此方法需要注意你是否在uc浏览器环境,如果你的页面在支付宝上使用尤其注意,因为支付宝使用的就是uc浏览器环境.</p> <h2 id="规范">规范</h2> <table class="standard-table"> <tbody> <tr> <th scope="col">Specification</th> <th scope="col">Status</th> <th scope="col">Comment</th> </tr> <tr> <td>{{SpecName('ES2015', '#sec-array.prototype.findindex', 'Array.prototype.findIndex')}}</td> <td>{{Spec2('ES2015')}}</td> <td>Initial definition.</td> </tr> <tr> <td>{{SpecName('ESDraft', '#sec-array.prototype.findIndex', 'Array.prototype.findIndex')}}</td> <td>{{Spec2('ESDraft')}}</td> <td></td> </tr> </tbody> </table> <h2 id="浏览器兼容">浏览器兼容</h2> <div class="hidden"> <p>The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p> </div> <p>{{Compat("javascript.builtins.Array.findIndex")}}</p> <h2 id="See_also" name="See_also">相关链接</h2> <ul> <li>{{jsxref("Array.prototype.find()")}}</li> <li>{{jsxref("Array.prototype.indexOf()")}}</li> </ul>