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
193
194
195
196
197
198
199
200
201
202
|
---
title: Array.prototype.some()
slug: Web/JavaScript/Reference/Global_Objects/Array/some
translation_of: Web/JavaScript/Reference/Global_Objects/Array/some
---
<div>{{JSRef}}</div>
<p>Il metodo <code><strong>some()</strong></code> verifica se almeno un elemento nell'array passa la verifica implementata dalla funzione fornita.</p>
<div class="note">
<p><strong>Note</strong>: Questo metodo ritorna <code>false</code> per qualsiasi condizione passata ad un array vuoto.</p>
</div>
<div>{{EmbedInteractiveExample("pages/js/array-some.html")}}</div>
<h2 id="Sintassi">Sintassi</h2>
<pre class="syntaxbox"><var>arr</var>.some(<var>callback</var>[, <var>thisArg</var>])</pre>
<h3 id="Parametri">Parametri</h3>
<dl>
<dt><code>callback</code></dt>
<dd>Funzione di test per ogni elemento, prende tre elementi:
<dl>
<dt><code>valoreCorrente</code></dt>
<dd>Il valore corrente dell'elemento che deve essere processato nell'array.</dd>
<dt><code>indice</code> {{Optional_inline}}</dt>
<dd>l'indice dell'elemento corrente dell'array.</dd>
<dt><code>array</code>{{Optional_inline}}</dt>
<dd>l'array completo alla quale è stato chiamato il <code>some().</code></dd>
</dl>
</dd>
<dt><code>thisArg</code>{{Optional_inline}}</dt>
<dd>Valore da usare come <code>this</code> quando si esegue la <code>callback</code>.</dd>
</dl>
<h3 id="Valore_ritornato">Valore ritornato</h3>
<p><code><strong>true</strong></code> se la funzione di callback ha ritornato un valore {{Glossary("truthy")}} per almeno un elemento nell'array; altrimenti, <code><strong>false</strong></code>.</p>
<h2 id="Descrizione">Descrizione</h2>
<p><code>some()</code> esegue la funzione di <code>callback</code> per ogni elemento presente nell'array finchè non ne trova uno dove la <code>callback</code> retorna un valore <em>truthy</em> (un valore che ritorna <code>true</code> se convertito in un Booleano). Se viene trovato un elemento di questo genere allora <code>some()</code> ritorna immediatamente <code>true</code>. altrimenti, <code>some()</code> ritorna <code>false</code>. <code>callback</code> viene invocato solamente solamente per gli elementi che hanno un valore assegnato; quindi non viene chiamato per elementi eliminati o mai assegnati.</p>
<p><code>callback</code> è invocato con tre argomenti: il valore dell'elemento, l'indice dell'elemento nell'array, e l'array dalla quale è stato invocato.</p>
<p>se viene passato un parametro <code>thisArg</code> al metodo <code>some()</code>, verrà usato come valore <code>this</code> per le callbacks. altrimenti, verrà usato il valore {{jsxref("undefined")}} come valore di <code>this</code>. Il valore di <code>this</code> nella <code>callback</code> è determinato in accordo con <a href="https://developer.mozilla.org/it/docs/Web/JavaScript/Reference/Operators/this">le normali regole per determinare il valore di this nelle funzioni</a>.</p>
<p><code>some()</code> non muta l'array dalla quale è stato evocato.</p>
<p>Il range di elementi processati da <code>some()</code> è impostato prima della prima chiamata alla <code>callback</code>. Gli elementi che vengono attaccati o aggiunti all'array dopo che è stata effettuata la chiamata al metodo <code>some()</code> non verranno tenuti in considerazione. Se al contrario un elemento viene cambiato prima che venga processato dalla <code>callback</code>, il valore passato sarà quello modificato. Elementi eliminati invece non verranno controllati. </p>
<h2 id="Esempi">Esempi</h2>
<h3 id="Testare_i_valori_all'interno_di_un_array">Testare i valori all'interno di un array</h3>
<p>L'esempio seguente testa se almeno un elemento dell'array è maggiore di 10.</p>
<pre class="brush: js">function isBiggerThan10(element, index, array) {
return element > 10;
}
[2, 5, 8, 1, 4].some(isBiggerThan10); // false
[12, 5, 8, 1, 4].some(isBiggerThan10); // true
</pre>
<h3 id="Testing_array_elements_using_arrow_functions">Testing array elements using arrow functions</h3>
<p><a href="/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions">Arrow functions</a> provide a shorter syntax for the same test.</p>
<pre class="brush: js">[2, 5, 8, 1, 4].some(x => x > 10); // false
[12, 5, 8, 1, 4].some(x => x > 10); // true
</pre>
<h3 id="Checking_whether_a_value_exists_in_an_array">Checking whether a value exists in an array</h3>
<p>To mimic the function of the <code>includes()</code> method, this custom function returns <code>true</code> if the element exists in the array:</p>
<pre class="brush: js">var fruits = ['apple', 'banana', 'mango', 'guava'];
function checkAvailability(arr, val) {
return arr.some(function(arrVal) {
return val === arrVal;
});
}
checkAvailability(fruits, 'kela'); // false
checkAvailability(fruits, 'banana'); // true</pre>
<h3 id="Checking_whether_a_value_exists_using_an_arrow_function">Checking whether a value exists using an arrow function</h3>
<pre class="brush: js">var fruits = ['apple', 'banana', 'mango', 'guava'];
function checkAvailability(arr, val) {
return arr.some(arrVal => val === arrVal);
}
checkAvailability(fruits, 'kela'); // false
checkAvailability(fruits, 'banana'); // true</pre>
<h3 id="Converting_any_value_to_Boolean">Converting any value to Boolean</h3>
<pre class="brush: js">var TRUTHY_VALUES = [true, 'true', 1];
function getBoolean(value) {
'use strict';
if (typeof value === 'string') {
value = value.toLowerCase().trim();
}
return TRUTHY_VALUES.some(function(t) {
return t === value;
});
}
getBoolean(false); // false
getBoolean('false'); // false
getBoolean(1); // true
getBoolean('true'); // true</pre>
<h2 id="Polyfill">Polyfill</h2>
<p><code>some()</code> was added to the ECMA-262 standard in the 5th edition; as such it may not be present in all implementations of the standard. You can work around this by inserting the following code at the beginning of your scripts, allowing use of <code>some()</code> in implementations which do not natively support it. This algorithm is exactly the one specified in ECMA-262, 5th edition, assuming {{jsxref("Object")}} and {{jsxref("TypeError")}} have their original values and that <code>fun.call</code> evaluates to the original value of {{jsxref("Function.prototype.call()")}}.</p>
<pre class="brush: js">// Production steps of ECMA-262, Edition 5, 15.4.4.17
// Reference: http://es5.github.io/#x15.4.4.17
if (!Array.prototype.some) {
Array.prototype.some = function(fun/*, thisArg*/) {
'use strict';
if (this == null) {
throw new TypeError('Array.prototype.some called on null or undefined');
}
if (typeof fun !== 'function') {
throw new TypeError();
}
var t = Object(this);
var len = t.length >>> 0;
var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
for (var i = 0; i < len; i++) {
if (i in t && fun.call(thisArg, t[i], i, t)) {
return true;
}
}
return false;
};
}
</pre>
<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('ES5.1', '#sec-15.4.4.17', 'Array.prototype.some')}}</td>
<td>{{Spec2('ES5.1')}}</td>
<td>Initial definition. Implemented in JavaScript 1.6.</td>
</tr>
<tr>
<td>{{SpecName('ES6', '#sec-array.prototype.some', 'Array.prototype.some')}}</td>
<td>{{Spec2('ES6')}}</td>
<td></td>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-array.prototype.some', 'Array.prototype.some')}}</td>
<td>{{Spec2('ESDraft')}}</td>
<td></td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<div>
<p>{{Compat("javascript.builtins.Array.some")}}</p>
</div>
<h2 id="See_also">See also</h2>
<ul>
<li>{{jsxref("Array.prototype.find()")}}</li>
<li>{{jsxref("Array.prototype.forEach()")}}</li>
<li>{{jsxref("Array.prototype.every()")}}</li>
<li>{{jsxref("TypedArray.prototype.some()")}}</li>
</ul>
|