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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
---
title: Array.prototype.find()
slug: Web/JavaScript/Reference/Global_Objects/Array/find
translation_of: Web/JavaScript/Reference/Global_Objects/Array/find
original_slug: Web/JavaScript/Referencia/Objectes_globals/Array/find
---
<div>{{JSRef}}</div>
<p>El mètode <code><strong>find()</strong></code> retorna un valor <strong>valor</strong> pertanyent a l'array si un element de l'array satisfà la funció de testeig donada. En cas contrari retornarà {{jsxref("undefined")}}.</p>
<p>Vegeu també el mètode {{jsxref("Array.findIndex", "findIndex()")}}, que retorna la posició a la qual s'ha trobat l'element que satisfà la funció de testeig, en comptes del seu valor.</p>
<h2 id="Sintaxi">Sintaxi</h2>
<pre class="syntaxbox"><code><var>arr</var>.find(<var>callback</var>[, <var>thisArg</var>])</code></pre>
<h3 id="Paràmetres">Paràmetres</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>find</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>find</code> retorna el valor de l'element trobat immediatament. En cas contrari <code>find</code> retornarà {{jsxref("undefined")}}. <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>find</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>find</code> no mutarà l'array des del que es crida.</p>
<p>El rang d'elemnets que <code>find</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>find</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_un_objecte_en_un_array_segons_el_valor_d'una_propietat">Trobar un objecte en un array segons el valor d'una propietat</h3>
<pre class="brush: js">var inventory = [
{name: 'apples', quantity: 2},
{name: 'bananas', quantity: 0},
{name: 'cherries', quantity: 5}
];
function findCherries(fruit) {
return fruit.name === 'cherries';
}
console.log(inventory.find(findCherries)); // { name: 'cherries', quantity: 5 }</pre>
<h3 id="Trobar_un_nombre_primer_en_un_array">Trobar un nombre primer en un array</h3>
<p>L'exemple següent troba un element dins l'array el valor del qual sigui un nombre primer (o bé retorna {{jsxref("undefined")}} 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].find(isPrime)); // undefined, no trobat
console.log([4, 5, 8, 12].find(isPrime)); // 5
</pre>
<h2 id="Polyfill">Polyfill</h2>
<p>Aquest mètode es va afegira la especificació 2015 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.find) {
Array.prototype.find = function(predicate) {
if (this === null) {
throw new TypeError('Array.prototype.find 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 value;
}
}
return undefined;
};
}
</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.find', 'Array.prototype.find')}}</td>
<td>{{Spec2('ES6')}}</td>
<td>Definició inicial.</td>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-array.prototype.find', 'Array.prototype.find')}}</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>Edge</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>12</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>Edge</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>12</td>
<td>{{CompatNo}}</td>
<td>8.0</td>
</tr>
</tbody>
</table>
</div>
<h2 id="Vegeu_també">Vegeu també</h2>
<ul>
<li>{{jsxref("Array.prototype.findIndex()")}}</li>
<li>{{jsxref("Array.prototype.every()")}}</li>
</ul>
|