Array.prototype.values()
Il metodo values()
restituisce un nuovo oggetto Array Iterator
che contiene i valori per ogni indice nell'array.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
var a = ['a', 'b', 'c', 'd', 'e'];
var iterator = a.values();
console.log(iterator.next().value); // a
console.log(iterator.next().value); // b
console.log(iterator.next().value); // c
console.log(iterator.next().value); // d
console.log(iterator.next().value); // e
Sintassi
arr.values()
Valore di ritorno
Un nuovo oggetto iteratore Array
.
Esempi
Iterazione utilizzando il
for...of
loop
var arr = ['a', 'b', 'c', 'd', 'e'];
var iterator = arr.values();
for (let letter of iterator) {
console.log(letter);
}
Array.prototype.values è una implementazionde di default di Array.prototype[Symbol.iterator].
Array.prototype.values === Array.prototype[Symbol.iterator] //true
TODO: please write about why we need it, use cases.
Specificazioni
Specification | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Array.prototype.values' in that specification. |
Standard | Definizione iniziale. |
ECMAScript (ECMA-262) The definition of 'Array.prototype.values' in that specification. |
Living Standard |
Browser compatibility
BCD tables only load in the browser