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
|
---
title: Array.prototype.findIndex()
slug: Web/JavaScript/Reference/Global_Objects/Array/findIndex
translation_of: Web/JavaScript/Reference/Global_Objects/Array/findIndex
original_slug: Web/JavaScript/Referencia/Objectes_globals/Array/findIndex
---
<div>{{JSRef}}</div>
<p>El mètode <code><strong>findIndex()</strong></code> retorna una <strong>posició</strong> de l'array si un element de l'array satisfà la funció de testeig donada. En cas contrari retornarà -1.</p>
<p>Vegeu també el mètode {{jsxref("Array.find", "find()")}}, que retorna el <strong>valor</strong> trobat dins l'array en comptes de la posició.</p>
<h2 id="Sintaxi">Sintaxi</h2>
<pre class="syntaxbox"><code><var>arr</var>.findIndex(<var>callback</var>[, <var>thisArg</var>])</code></pre>
<h3 id="Parameters">Parameters</h3>
<dl>
<dt><code>callback</code></dt>
<dd>Funció que s'executarà per a cada valor de l'array, rep tres arguments:
<dl>
<dt><code>element</code></dt>
<dd>L'element de l'array que s'està processant actualment.</dd>
<dt><code>posició</code></dt>
<dd>La posició de l'array que s'està processant actualment.</dd>
<dt><code>array</code></dt>
<dd>L'array des del qual s'ha cridat el mètode <code>find</code>.</dd>
</dl>
</dd>
<dt><code>thisArg</code></dt>
<dd>Opcional. L'objecte a utilitzar com a <code>this</code> mentre s'executi <code>callback</code>.</dd>
</dl>
<h2 id="Descripció">Descripció</h2>
<p>El mètode <code>findIndex</code> executa la funció <code>callback</code> un cop per a cada element present a l'array fins que trobi un on <code>callback</code> retorni <code>true</code>. Si es troba aquest element el mètode <code>findIndex</code> retorna la posició de l'element trobat immediatament. En cas contrari <code>findIndex</code> retornarà -1. <code>callback</code> només serà invocada per a posicions de l'array que tinguin valors assignats; no serà invoada per a posicions que s'hagin eliminat o que mai hagin tingut assignat un valor.</p>
<p>La invocaicó de <code>callback</code> té tres arguments: el valor de l'element, la posició de l'element i l'objecte array que està sent recorregut.</p>
<p>Si es proporciona el paràmetre <code>thisArg</code> al cridar el mètode <code>findIndex</code>, aquest serà utilitzat com a <code>this</code> per a cada invocació del mètode <code>callback</code>. En cas de no ser proporcionat s'utilitzarà {{jsxref("undefined")}}.</p>
<p><code>findIndex</code> no mutarà l'array des del que es crida.</p>
<p>El rang d'elemnets que <code>findIndex</code> processarà es determina abans de la primera invocació a <code>callback</code>. Els elements afegits a l'array després de la crida a <code>findIndex</code> no seran visitats per <code>callback</code>. Si un element existent, no visitat encara, rep un altre valor, el valor percebut per <code>callback</code> serà aquell que tingui l'element al ser visitat; els elements visitats no són visitats.</p>
<h2 id="Exemples">Exemples</h2>
<h3 id="Trobar_la_posició_d'un_nombre_primer_dins_un_array">Trobar la posició d'un nombre primer dins un array</h3>
<p>L'exemple següent trobarà la posició d'un element de l'array que sigui un nombre primer (o bé retornarà -1 si no n'hi ha cap).</p>
<pre class="brush: js">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>
<p>Aquest mètode es va afegir a la especificació 6 de l'ECMAScript i pot no estar disponible encara en algunes implementacions de JavaScript. Tot i així es pot utilitzar el codi següent per a utilitzar-lo en entorns on no estigui disponible:</p>
<pre class="brush: js">if (!Array.prototype.findIndex) {
Array.prototype.findIndex = function(predicate) {
if (this === null) {
throw new TypeError('Array.prototype.findIndex called on null or undefined');
}
if (typeof predicate !== 'function') {
throw new TypeError('predicate must be a function');
}
var list = Object(this);
var length = list.length >>> 0;
var thisArg = arguments[1];
var value;
for (var i = 0; i < length; i++) {
value = list[i];
if (predicate.call(thisArg, value, i, list)) {
return i;
}
}
return -1;
};
}
</pre>
<h2 id="Especificacions">Especificacions</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Especificació</th>
<th scope="col">Estat</th>
<th scope="col">Comentaris</th>
</tr>
<tr>
<td>{{SpecName('ES6', '#sec-array.prototype.findIndex', 'Array.prototype.findIndex')}}</td>
<td>{{Spec2('ES6')}}</td>
<td>Definició inicial.</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="Compatibilitat_amb_navegadors">Compatibilitat amb navegadors</h2>
<div>{{CompatibilityTable}}</div>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Característica</th>
<th>Chrome</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>Suport bàsic</td>
<td>{{CompatChrome(45.0)}}</td>
<td>{{CompatGeckoDesktop("25.0")}}</td>
<td>{{CompatNo}}</td>
<td>{{CompatNo}}</td>
<td>{{CompatSafari("7.1")}}</td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Característica</th>
<th>Android</th>
<th>Chrome for Android</th>
<th>Firefox Mobile (Gecko)</th>
<th>IE Mobile</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
</tr>
<tr>
<td>Suport bàsic</td>
<td>{{CompatNo}}</td>
<td>{{CompatNo}}</td>
<td>{{CompatGeckoMobile("25.0")}}</td>
<td>{{CompatNo}}</td>
<td>{{CompatNo}}</td>
<td>8.0</td>
</tr>
</tbody>
</table>
</div>
<h2 id="Vegeu_també">Vegeu també</h2>
<ul>
<li>{{jsxref("Array.prototype.find()")}}</li>
<li>{{jsxref("Array.prototype.indexOf()")}}</li>
</ul>
|