1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
---
title: Array.prototype.values()
slug: Web/JavaScript/Reference/Global_Objects/Array/values
tags:
- Array
- ECMAScript 2015
- Iterator
- JavaScript
- Prototype
- 数组
- 方法
- 迭代
translation_of: Web/JavaScript/Reference/Global_Objects/Array/values
---
<div>{{JSRef}}</div>
<p><strong><code>values()</code></strong> 方法返回一个新的 <strong><code>Array Iterator</code></strong> 对象,该对象包含数组每个索引的值</p>
<div>{{EmbedInteractiveExample("pages/js/array-values.html")}}</div>
<h2 id="语法">语法</h2>
<pre class="syntaxbox"><var>arr</var>.values()</pre>
<h3 id="返回值">返回值</h3>
<p>一个新的 {{jsxref("Array")}} 迭代对象。</p>
<h2 id="示例">示例</h2>
<h3 id="使用_for...of_循环进行迭代">使用 <code>for...of</code> 循环进行迭代</h3>
<pre class="brush: js">let arr = ['w', 'y', 'k', 'o', 'p'];
let eArr = arr.values();
for (let letter of eArr) {
console.log(letter);
} //"w" "y "k" "o" "p"</pre>
<p><strong>Array.prototype.values</strong> 是 <strong>Array.prototype[Symbol.iterator] </strong>的默认实现。</p>
<pre>Array.prototype.values === Array.prototype[Symbol.iterator] // true </pre>
<h3 id="使用_.next_迭代">使用 <code>.next()</code> 迭代</h3>
<pre>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 }
iterator.next().value; // undefined</pre>
<div class="blockIndicator warning">
<p>一次性:数组迭代器是一次性的,或者说临时对象</p>
</div>
<p>例子:</p>
<pre>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</pre>
<p><strong>解释:</strong> 当 <code>next().done=true</code> 或 <code>currentIndex>length</code> 时, <code>for..of</code> 循环结束。参见 <a href="/en-US/docs/Web/JavaScript/Reference/Iteration_protocols">Iteration protocols</a> 。</p>
<p><strong>值</strong>: 数组迭代器中存储的是原数组的地址,而不是数组元素值。</p>
<pre>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"
</pre>
<div class="blockIndicator note">
<p>如果数组中元素改变,那么迭代器的值也会改变</p>
</div>
<h2 id="规范">规范</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">规范名称</th>
<th scope="col">规范状态</th>
<th scope="col">备注</th>
</tr>
<tr>
<td>{{SpecName('ES6', '#sec-array.prototype.values', 'Array.prototype.values')}}</td>
<td>{{Spec2('ES6')}}</td>
<td>首次定义</td>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-array.prototype.values', 'Array.prototype.values')}}</td>
<td>{{Spec2('ESDraft')}}</td>
<td></td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<p>{{Compat("javascript.builtins.Array.values")}}</p>
<h2 id="相关链接">相关链接</h2>
<ul>
<li>{{jsxref("Array.prototype.keys()")}}</li>
<li>{{jsxref("Array.prototype.entries()")}}</li>
<li>{{jsxref("Array.prototype.forEach()")}}</li>
<li>{{jsxref("Array.prototype.every()")}}</li>
<li>{{jsxref("Array.prototype.some()")}}</li>
</ul>
|