Array.prototype[@@iterator]()
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since septembre 2016.
La valeur initiale de la propriété @@iterator
correspond à la valeur initiale fournie par l'itérateur values
.
Syntaxe
js
arr[Symbol.iterator]();
Valeur de retour
La première valeur fournie par values()
. Si on utilise arr[Symbol.iterator]
(sans les parenthèses) le navigateur renverra par défaut la fonction values()
.
Exemples
>Parcourir un tableau avec une boucle for...of
js
var arr = ["w", "y", "k", "o", "p"];
var eArr = arr[Symbol.iterator]();
// il est nécessaire que l'environnement supporte
// les boucles for..of et les variables
// utilisées avec let ou const ou var
for (let letter of eArr) {
console.log(letter);
}
Parcourir un tableau avec next
js
var arr = ["w", "y", "k", "o", "p"];
var eArr = arr[Symbol.iterator]();
console.log(eArr.next().value); // w
console.log(eArr.next().value); // y
console.log(eArr.next().value); // k
console.log(eArr.next().value); // o
console.log(eArr.next().value); // p
Spécifications
Specification |
---|
ECMAScript® 2026 Language Specification> # sec-array.prototype-%symbol.iterator%> |
Compatibilité des navigateurs
Loading…