--- title: Array.prototype.every() slug: Web/JavaScript/Reference/Global_Objects/Array/every translation_of: Web/JavaScript/Reference/Global_Objects/Array/every ---
Il metodo every()
controlla che tutti gli elementi all'interno dell'array passino il test implementato dalla funzione fornita.
Il codice sorgente per questo esempio interattivo è salvato all'interno di una repository GitHub. Se vuoi contribuire al progetto sugli esempi interattivi, perfavore clona https://github.com/mdn/interactive-examples e mandaci una pull request.
arr.every(callback[, thisArg])
callback
currentValue
(required)index
{{Optional_inline}}array
{{Optional_inline}}every
chiamato.thisArg
{{Optional_inline}}this
durante l'esecuzione della callback
.true
se la funzione callback ritorna un valore {{Glossary("truthy")}} per ciascun elemento dell'array; altrimenti, false
.
Il metodo every
esegue la funzione callback
fornita una volta per ciascun elemento presente all'interno dell'array finché non ne trova uno per il quale la callback
ritorna un valore {{Glossary("falsy")}}. Altrimenti, se la callback
ritorna un valore {{Glossary("truthy")}} per tutti gli elementi, every
ritorna true
. callback
viene invocata solo per gli indici dell'array che hanno un valore assegnato; non viene invocata per gli indici che sono stati eliminati o ai quali non è mai stato assegnato un valore.
callback
viene invocata con tre argomenti: il valore dell'elemento, l'indice dell'elemento, e l'Array oggetto che viene attraversato.
Se il parametro thisArg
viene fornito a every
, esso verrà usato come valore this
per la callback
. Altrimenti, il valore undefined
verrà usato al suo posto come valore this
. Il valore this
, ultimo osservabile dalla callback
, viene determinato secondo le usuali regole per determinare il this visto da una funzione.
every
non modifica l'array in cui viene chiamato.
Il range di elementi processati da every
viene impostato prima della prima invocazione di callback
. Gli elementi che vengono appesi all'inizio dell'array dopo la chiamata a every
non verranno visitati dalla callback
. Se gli elementi esistenti dell'array vengono cambiati, il loro valore passato a callback
sarà il valore al momento in cui every
li visiterà; gli elementi cancellati non sono visitati.
every
agisce come il quantificatore "for all" in matematica. In particolare, per un array vuoto, esso ritorna true. (E' vacuously true che tutti gli elementi dell' empty set soddisfano qualsiasi condizione data.)
Il seguente esempio controlla che tutti gli elementi dell'array siano maggiori di 10.
function isBigEnough(element, index, array) { return element >= 10; } [12, 5, 8, 130, 44].every(isBigEnough); // false [12, 54, 18, 130, 44].every(isBigEnough); // true
Arrow functions forniscono una sintassi più breve per lo stesso test.
[12, 5, 8, 130, 44].every(x => x >= 10); // false [12, 54, 18, 130, 44].every(x => x >= 10); // true
every
è stato aggiunto allo standard ECMA-262 nella 5a edizione; per questo motivo potrebbe non essere presente in altre implementazioni dello standard. Potrai aggirare il problema inserendo il seguente codice all'inizio dei tuoi scripts, permettendo così l'utilizzo di every
in implementazioni che non lo supportano nativamente. Questo algoritmo è esattamente quello specificato in ECMA-262, 5a edizione, assumendo che Object
e TypeError
abbiano i loro valori originali e che callbackfn.call
valuti sul valore originale di {{jsxref("Function.prototype.call")}}.
if (!Array.prototype.every) { Array.prototype.every = function(callbackfn, thisArg) { 'use strict'; var T, k; if (this == null) { throw new TypeError('this is null or not defined'); } // 1. Let O be the result of calling ToObject passing the this // value as the argument. var O = Object(this); // 2. Let lenValue be the result of calling the Get internal method // of O with the argument "length". // 3. Let len be ToUint32(lenValue). var len = O.length >>> 0; // 4. If IsCallable(callbackfn) is false, throw a TypeError exception. if (typeof callbackfn !== 'function') { throw new TypeError(); } // 5. If thisArg was supplied, let T be thisArg; else let T be undefined. if (arguments.length > 1) { T = thisArg; } // 6. Let k be 0. k = 0; // 7. Repeat, while k < len while (k < len) { var kValue; // a. Let Pk be ToString(k). // This is implicit for LHS operands of the in operator // b. Let kPresent be the result of calling the HasProperty internal // method of O with argument Pk. // This step can be combined with c // c. If kPresent is true, then if (k in O) { // i. Let kValue be the result of calling the Get internal method // of O with argument Pk. kValue = O[k]; // ii. Let testResult be the result of calling the Call internal method // of callbackfn with T as the this value and argument list // containing kValue, k, and O. var testResult = callbackfn.call(T, kValue, k, O); // iii. If ToBoolean(testResult) is false, return false. if (!testResult) { return false; } } k++; } return true; }; }
Specifica | Stato | Commento |
---|---|---|
{{SpecName('ES5.1', '#sec-15.4.4.16', 'Array.prototype.every')}} | {{Spec2('ES5.1')}} | Definizione iniziale. Implementata in JavaScript 1.6. |
{{SpecName('ES6', '#sec-array.prototype.every', 'Array.prototype.every')}} | {{Spec2('ES6')}} | |
{{SpecName('ESDraft', '#sec-array.prototype.every', 'Array.prototype.every')}} | {{Spec2('ESDraft')}} |
{{Compat("javascript.builtins.Array.every")}}