--- title: Array.prototype.some() slug: Web/JavaScript/Reference/Global_Objects/Array/some translation_of: Web/JavaScript/Reference/Global_Objects/Array/some ---
Il metodo some()
verifica se almeno un elemento nell'array passa la verifica implementata dalla funzione fornita.
Note: Questo metodo ritorna false
per qualsiasi condizione passata ad un array vuoto.
arr.some(callback[, thisArg])
callback
valoreCorrente
indice
{{Optional_inline}}array
{{Optional_inline}}some().
thisArg
{{Optional_inline}}this
quando si esegue la callback
.true
se la funzione di callback ha ritornato un valore {{Glossary("truthy")}} per almeno un elemento nell'array; altrimenti, false
.
some()
esegue la funzione di callback
per ogni elemento presente nell'array finchè non ne trova uno dove la callback
retorna un valore truthy (un valore che ritorna true
se convertito in un Booleano). Se viene trovato un elemento di questo genere allora some()
ritorna immediatamente true
. altrimenti, some()
ritorna false
. callback
viene invocato solamente solamente per gli elementi che hanno un valore assegnato; quindi non viene chiamato per elementi eliminati o mai assegnati.
callback
è invocato con tre argomenti: il valore dell'elemento, l'indice dell'elemento nell'array, e l'array dalla quale è stato invocato.
se viene passato un parametro thisArg
al metodo some()
, verrà usato come valore this
per le callbacks. altrimenti, verrà usato il valore {{jsxref("undefined")}} come valore di this
. Il valore di this
nella callback
è determinato in accordo con le normali regole per determinare il valore di this nelle funzioni.
some()
non muta l'array dalla quale è stato evocato.
Il range di elementi processati da some()
è impostato prima della prima chiamata alla callback
. Gli elementi che vengono attaccati o aggiunti all'array dopo che è stata effettuata la chiamata al metodo some()
non verranno tenuti in considerazione. Se al contrario un elemento viene cambiato prima che venga processato dalla callback
, il valore passato sarà quello modificato. Elementi eliminati invece non verranno controllati.
L'esempio seguente testa se almeno un elemento dell'array è maggiore di 10.
function isBiggerThan10(element, index, array) { return element > 10; } [2, 5, 8, 1, 4].some(isBiggerThan10); // false [12, 5, 8, 1, 4].some(isBiggerThan10); // true
Arrow functions provide a shorter syntax for the same test.
[2, 5, 8, 1, 4].some(x => x > 10); // false [12, 5, 8, 1, 4].some(x => x > 10); // true
To mimic the function of the includes()
method, this custom function returns true
if the element exists in the array:
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
var fruits = ['apple', 'banana', 'mango', 'guava']; function checkAvailability(arr, val) { return arr.some(arrVal => val === arrVal); } checkAvailability(fruits, 'kela'); // false checkAvailability(fruits, 'banana'); // true
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
some()
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 some()
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 fun.call
evaluates to the original value of {{jsxref("Function.prototype.call()")}}.
// 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; }; }
Specification | Status | Comment |
---|---|---|
{{SpecName('ES5.1', '#sec-15.4.4.17', 'Array.prototype.some')}} | {{Spec2('ES5.1')}} | Initial definition. Implemented in JavaScript 1.6. |
{{SpecName('ES6', '#sec-array.prototype.some', 'Array.prototype.some')}} | {{Spec2('ES6')}} | |
{{SpecName('ESDraft', '#sec-array.prototype.some', 'Array.prototype.some')}} | {{Spec2('ESDraft')}} |
{{Compat("javascript.builtins.Array.some")}}