Array.prototype.values() - JavaScript 编辑
The values()
method returns a new Array Iterator
object that contains the values for each index in the 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.
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" "e"
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 }
iteraror.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" "e"
for (let letter of iterator) {
console.log(letter);
} // undefined
reason: When next().done=true
or currentIndex>length
the for..of
loop ends. See Iteration protocols.
Value: there are no values stored in the array Iterator object; instead it stores the address of the array used in its creation and so depends on the values stored in 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.
TODO: please write about why we need it, use cases.
Specifications
Specification |
---|
ECMAScript (ECMA-262) The definition of 'Array.prototype.values' in that specification. |
Browser compatibility
BCD tables only load in the browser
See also
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论