1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
---
title: Array.prototype.findIndex()
slug: Web/JavaScript/Reference/Global_Objects/Array/findIndex
translation_of: Web/JavaScript/Reference/Global_Objects/Array/findIndex
---
<div>{{JSRef}}</div>
<p>FindIndex () yöntemi, sağlanan test işlevini karşılayan dizideki ilk öğenin dizinini döndürür. Aksi takdirde -1 iade edilir.</p>
<div>{{EmbedInteractiveExample("pages/js/array-findindex.html")}}</div>
<p class="hidden">Bu interaktif örneğin için kaynak bir GitHub deposunda saklanır. Etkileşimli örnek projesine katkıda bulunmak isterseniz, lütfen https://github.com/mdn/interactive-examples klonlayın ve bize bir çekme isteği gönderin.</p>
<div> </div>
<p>Ayrıca, dizinde bulunan dizinin yerine bulunan bir öğenin değerini döndüren {{jsxref ("Array.find", "find ()")}} yöntemine de bakın.</p>
<h2 id="Syntax">Syntax</h2>
<pre class="syntaxbox"><var>arr</var>.findIndex(<var>callback</var>[, <var>thisArg</var>])</pre>
<h3 id="Parameters">Parameters</h3>
<dl>
<dt><code>callback</code></dt>
<dd>Function to execute on each value in the array, taking three arguments:
<dl>
<dt><code>element</code></dt>
<dd>The current element being processed in the array.</dd>
<dt><code>index</code>{{optional_inline}}</dt>
<dd>The index of the current element being processed in the array.</dd>
<dt><code>array</code>{{optional_inline}}</dt>
<dd>The array <code>findIndex</code> was called upon.</dd>
</dl>
</dd>
<dt><code>thisArg</code>{{optional_inline}}</dt>
<dd>Optional. Object to use as <code>this</code> when executing <code>callback</code>.</dd>
</dl>
<h3 id="Return_value">Return value</h3>
<p>An index in the array if an element passes the test; otherwise, <strong>-1</strong>.</p>
<h2 id="Description">Description</h2>
<p>The <code>findIndex</code> method executes the <code>callback</code> function once for every array index <code>0..length-1</code> (inclusive) in the array until it finds one where <code>callback</code> returns a truthy value (a value that coerces to <code>true</code>). If such an element is found, <code>findIndex</code> immediately returns the index for that iteration. If the callback never returns a truthy value or the array's <code>length</code> is 0, <code>findIndex</code> returns -1. Unlike some other array methods such as Array#some, in sparse arrays the <code>callback</code> <strong>is</strong> called even for indexes of entries not present in the array.</p>
<p><code>callback</code> is invoked with three arguments: the value of the element, the index of the element, and the Array object being traversed.</p>
<p>If a <code>thisArg</code> parameter is provided to <code>findIndex</code>, it will be used as the <code>this</code> for each invocation of the <code>callback</code>. If it is not provided, then {{jsxref("undefined")}} is used.</p>
<p><code>findIndex</code> does not mutate the array on which it is called.</p>
<p>The range of elements processed by <code>findIndex</code> is set before the first invocation of <code>callback</code>. Elements that are appended to the array after the call to <code>findIndex</code> begins will not be visited by <code>callback</code>. If an existing, unvisited element of the array is changed by <code>callback</code>, its value passed to the visiting <code>callback</code> will be the value at the time that <code>findIndex</code> visits that element's index; elements that are deleted are still visited.</p>
<h2 id="Examples">Examples</h2>
<h3 id="Find_the_index_of_a_prime_number_in_an_array">Find the index of a prime number in an array</h3>
<p>The following example finds the index of an element in the array that is a prime number (or returns -1 if there is no prime number).</p>
<pre class="brush: js">function isPrime(element, index, array) {
var start = 2;
while (start <= Math.sqrt(element)) {
if (element % start < 1) {
return false;
} else {
start++;
}
}
return element > 1;
}
console.log([4, 6, 8, 12].findIndex(isPrime)); // -1, not found
console.log([4, 6, 7, 12].findIndex(isPrime)); // 2
</pre>
<h3 id="Find_index_using_arrow_function">Find index using arrow function</h3>
<p>The following example finds the index of a fruit using an arrow function.</p>
<pre class="brush: js">const fruits = ["apple", "banana", "cantaloupe", "blueberries", "grapefruit"];
const index = fruits.findIndex(fruit => fruit === "blueberries");
console.log(index); // 3
console.log(fruits[index]); // blueberries
</pre>
<h2 id="Polyfill">Polyfill</h2>
<pre class="brush: js">// 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;
},
configurable: true,
writable: true
});
}
</pre>
<p>If you need to support truly obsolete JavaScript engines that don't support <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty">Object.defineProperty</a></code>, it's best not to polyfill <code>Array.prototype</code> methods at all, as you can't make them non-enumerable.</p>
<h2 id="Specifications">Specifications</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="Browser_compatibility">Browser compatibility</h2>
<div>
<p>{{Compat("javascript.builtins.Array.findIndex")}}</p>
</div>
<h2 id="See_also">See also</h2>
<ul>
<li>{{jsxref("Array.prototype.find()")}}</li>
<li>{{jsxref("Array.prototype.indexOf()")}}</li>
</ul>
|