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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
|
---
title: for...of
slug: Web/JavaScript/Reference/Statements/for...of
translation_of: Web/JavaScript/Reference/Statements/for...of
---
<p>{{jsSidebar("Statements")}}</p>
<p><code><font face="Open Sans, Arial, sans-serif">Il </font><strong>costrutto for...of</strong></code> crea un ciclo con gli <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#iterable">oggetti iterabil</a>i (inclusi {{jsxref("Array")}}, {{jsxref("Map")}}, {{jsxref("Set")}}, {{jsxref("String")}}, {{jsxref("TypedArray")}}, <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope/arguments">argomenti</a> di oggetti e così via), iterando le istruzioni per ogni valore di ogni proprietà.</p>
<h2 id="Sintassi">Sintassi</h2>
<pre>for (<em>variabile</em> of <em>oggettoIterabile</em>) {
<em>istruzioni
</em>}
</pre>
<dl>
<dt><code>variabile</code></dt>
<dd><br>
Questa variabile assume il valore di una proprietà in ogni ciclo.</dd>
<dt><code>oggettoIterabile</code></dt>
<dd>Oggetto le cui proprietà sono iterate.</dd>
</dl>
<h2 id="Esempi">Esempi</h2>
<h3 id="Iterare_un_jsxref(Array)">Iterare un {{jsxref("Array")}}:</h3>
<pre>let array = [10, 20, 30];
for (let valore of array) {
console.log(valore);
}
// Output:
// 10
// 20
// 30
</pre>
<p><code>Si può utilizzare <a href="/en-US/docs/Web/JavaScript/Reference/Statements/const">const</a></code> anzichè <a href="/en-US/docs/Web/JavaScript/Reference/Statements/let"><code>let</code></a> se 'value' non cambia valore durante il ciclo.</p>
<pre>let iterable = [10, 20, 30];
for (const value of iterable) {
console.log(value); // il valore di value di questo ciclo rimarrà costante
value++; // operazione non consentita.
}
// 10
// 20
// 30
</pre>
<h3 id="Iterare_un_oggetto_jsxref(String)">Iterare un oggetto {{jsxref("String")}}:</h3>
<pre>let iterable = "boo";
for (let value of iterable) {
console.log(value);
}
// output:
// "b"
// "o"
// "o"
</pre>
<h3 id="Iterare_un_oggetto_jsxref(TypedArray)">Iterare un oggetto {{jsxref("TypedArray")}}:</h3>
<pre>let iterable = new Uint8Array([0x00, 0xff]);
for (let value of iterable) {
console.log(value);
}
// 0
// 255
</pre>
<h3 id="Iterare_un_oggetto_jsxref(Map)">Iterare un oggetto {{jsxref("Map")}}:</h3>
<pre>let iterable = new Map([["a", 1], ["b", 2], ["c", 3]]);
for (let entry of iterable) {
console.log(entry);
}
// [a, 1]
// [b, 2]
// [c, 3]
for (let [key, value] of iterable) {
console.log(value);
}
// 1
// 2
// 3
</pre>
<h3 id="Iterare_un_oggetto_jsxref(Set)">Iterare un oggetto {{jsxref("Set")}}:</h3>
<pre>let iterable = new Set([1, 1, 2, 2, 3, 3]);
for (let value of iterable) {
console.log(value);
}
// 1
// 2
// 3
</pre>
<h3 id="Iterare_un_DOM_collection">Iterare un DOM collection</h3>
<div>
<p>Iterare un DOM collection come {{domxref("NodeList")}}: il seguente esempio aggiunge una classe 'read' ai paragrafi che sono discendenti diretti di un elemento <em>article</em>:</p>
</div>
<div class="warning">
<p>Nota: Questo costrutto funziona soltanto con le piattaforme che implementano NodeList.prototype[Symbol.iterator]</p>
</div>
<div>
<pre>let articleParagraphs = document.querySelectorAll("article > p");
for (let paragraph of articleParagraphs) {
paragraph.classList.add("read");
}
</pre>
</div>
<h3 id="Iterare_generatori">Iterare generatori</h3>
<p>Si possono iterare i <a href="/en-US/docs/Web/JavaScript/Reference/Statements/function*">generatori</a>:</p>
<pre>function* fibonacci() { // generatore di funzione
let [prev, curr] = [0, 1];
while (true) {
[prev, curr] = [curr, prev + curr];
yield curr;
}
}
for (let n of fibonacci()) {
console.log(n);
// tronca la sequenza a 1000
if (n >= 1000) {
break;
}
}
</pre>
<h3 id="Differerenze_tra_for...of_e_for...in">Differerenze tra <code>for...of</code> e <code>for...in</code></h3>
<p>Il <code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/for...in" title="en-US/docs/JavaScript/Reference/Statements/for...in">for...in</a></code> itera tutte le proprietà enumerabili di un oggetto.</p>
<p>Il <code>for...of</code> è specifico per le <strong>collezioni</strong>, piuttosto che tutti gli oggetti. Itera tutti gli oggetti di qualsiasi collection che abbia la proprietà <code>[Symbol.iterator]</code>.</p>
<p>Il seguente esempio mostra le differenze tra <code>for...of</code> e <code>for...in</code>.</p>
<pre>Object.prototype.objCustom = function () {};
Array.prototype.arrCustom = function () {};
let iterable = [3, 5, 7];
iterable.foo = "hello";
for (let i in iterable) {
console.log(i); // logs 0, 1, 2, "foo", "arrCustom", "objCustom"
}
for (let i of iterable) {
console.log(i); // logs 3, 5, 7
}
</pre>
<h2 id="Specifiche">Specifiche</h2>
<table>
<tbody>
<tr>
<th scope="col">Specifica</th>
<th scope="col">Stato</th>
<th scope="col">Commenti</th>
</tr>
<tr>
<td>{{SpecName('ES6', '#sec-for-in-and-for-of-statements', 'for...of statement')}}</td>
<td>{{Spec2('ES6')}}</td>
<td>Definizione iniziale.</td>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-for-in-and-for-of-statements', 'for...of statement')}}</td>
<td>{{Spec2('ESDraft')}}</td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="Compatibilità_con_i_browser">Compatibilità con i browser</h2>
<p>{{CompatibilityTable}}</p>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Chrome</th>
<th>Firefox (Gecko)</th>
<th>Edge</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>Basic support</td>
<td>{{CompatChrome(38)}} <a href="#Chrome_note_1">[1]</a><br>
{{CompatChrome(51)}} <a href="#Chrome_note_3">[3]</a></td>
<td>{{CompatGeckoDesktop("13")}} <a href="#Gecko_note_2">[2]</a></td>
<td>12</td>
<td>25</td>
<td>7.1</td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Android</th>
<th>Chrome for Android</th>
<th>Firefox Mobile (Gecko)</th>
<th>IE Mobile</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
</tr>
<tr>
<td>Basic support</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatChrome(38)}} [1]</td>
<td>{{CompatGeckoMobile("13")}} [2]</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatUnknown}}</td>
<td>8</td>
</tr>
</tbody>
</table>
</div>
<p><a name="Chrome_note_1">[1]</a> Da Chrome 29 a Chrome 37 questa caratteristica era disponibile nel menu preferenze. In chrome://flags/#enable-javascript-harmony, bisognava attivare “Enable Experimental JavaScript”.</p>
<p><a name="Gecko_note_2">[2]</a> Da Gecko 17 (Firefox 17 / Thunderbird 17 / SeaMonkey 2.14) a Gecko 26 (Firefox 26 / Thunderbird 26 / SeaMonkey 2.23 / Firefox OS 1.2) la proprietà iterator era usata (<a href="https://bugzilla.mozilla.org/show_bug.cgi?id=907077">bug 907077</a>), e da Gecko 27 a Gecko 35 era utilizzato <code>"@@iterator"</code> placeholder. In Gecko 36 (Firefox 36 / Thunderbird 36 / SeaMonkey 2.33), il simbolo <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol">symbolo</a> <code>@@iterator</code> fu implementato (<a href="https://bugzilla.mozilla.org/show_bug.cgi?id=918828">bug 918828</a>).</p>
<p><a name="Chrome_note_3">[3]</a> Il supporto dell'iteration of objects è stato aggiunto in Chrome 51.</p>
<h2 id="Note">Note</h2>
<p>Se alcuni collegamenti su questa pagina fossero segnati in rosso e si volesse visualizzarli, essi sono dispoiìnibili solo in lingua inglese. In tal caso basta cambiare la lingua di questo stesso articolo.</p>
<h2 id="See_also">See also</h2>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for_each...in">for each...in</a> - a similar statement, but iterates over the values of object's properties, rather than the property names themselves (deprecated).</li>
<li>{{jsxref("Array.prototype.forEach()")}}</li>
<li><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/forEach">Map.prototype.forEach()</a></li>
</ul>
<p> </p>
|