Array.prototype.values()

U leest de Engelstalige versie van deze inhoud, omdat er nog geen vertaling voor deze taal beschikbaar is. Help ons dit artikel te vertalen!

The values() method returns a new Array Iterator object that contains the values for each index in the array.

Syntax

arr.values()

Return value

A new Array iterator object.

Examples

Iteration using for...of loop

var arr = ['a', 'b', 'c', 'd', 'e'];
var iterator = arr.values();

for (let letter of iterator) {
  console.log(letter);           
}  //"a" "b" "c" "d"

Array.prototype.values is default implementation of Array.prototype[Symbol.iterator].

Array.prototype.values === Array.prototype[Symbol.iterator]      //true

Iteration using .next()

var arr = ['a', 'b', 'c', 'd', 'e'];
var iterator = arr.values(); 
iterator.next();               // Object { value: "a", done: false }
iterator.next().value;         // "b"
iterator.next()["value"];      // "c"
iterator.next();               // Object { value: "d", done: false }
iterator.next();               // Object { value: "e", done: false }
iterator.next();               // Object { value: undefined, done: true } 
iteraroe.next().value;         // undefined 

One-use: the array iterator object is one use or temporary object

example:

var arr = ['a', 'b', 'c', 'd', 'e'];
 var iterator = arr.values();
 for (let letter of iterator) {
 console.log(letter); 
} //"a" "b" "c" "d"
for (let letter of iterator) {
console.log(letter);
} // undefined 

reason: When next().done=true  or  currentIndex>length the for..of loop end. see Iteration protocols.

Value: there is no values stored in the array Iterator object,instead its store the adresse of the array used in creation of it so its depend on the values stored on that array.

var arr = ['a', 'b', 'c', 'd', 'e']; 
var iterator = arr.values();
console.log(iterator);        // Array Iterator {  }
iterator.next().value;        // "a"
arr[1]='n';                 
iterator.next().value;        //  "n"

if the values in the array changed the array iterator object values change too

Specifications

Specification Status Comment
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'Array.prototype.values' in that specification.
Standard Initial definition.
ECMAScript Latest Draft (ECMA-262)
The definition of 'Array.prototype.values' in that specification.
Draft

Browser compatibility

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
valuesChrome Full support 66Edge Full support 12Firefox Full support 60IE No support NoOpera Full support 53Safari Full support 9WebView Android Full support 66Chrome Android Full support 66Firefox Android Full support 60Opera Android Full support 47Safari iOS Full support 9Samsung Internet Android No support Nonodejs Full support 10.9.0
Full support 10.9.0
Full support 6.5.0
Notes Disabled
Notes The --harmony-array-prototype-values flag is required; the --harmony flag is not sufficient in this case.
Disabled From version 6.5.0: this feature is behind the --harmony-array-prototype-values runtime flag.
No support 0.12 — 4.0.0

Legend

Full support  
Full support
No support  
No support
See implementation notes.
See implementation notes.
User must explicitly enable this feature.
User must explicitly enable this feature.

See also