aboutsummaryrefslogtreecommitdiff
path: root/files/it/web/javascript/reference/global_objects/set/index.html
blob: c8de5d83f6b7ab60fc8c7bbd04a099c7900a2c15 (plain)
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
---
title: Set
slug: Web/JavaScript/Reference/Global_Objects/Set
translation_of: Web/JavaScript/Reference/Global_Objects/Set
---
<div>{{JSRef}}</div>

<div>L'oggetto <strong>Set</strong> permette di memorizzare valori <em>unici </em>di qualunque tipo, che siano {{Glossary("Primitive", "valori primitivi")}} o riferimenti ad oggetti.</div>

<div> </div>

<h2 id="Sintassi">Sintassi</h2>

<pre class="syntaxbox">new Set([iterabile]);</pre>

<h3 id="Parametri">Parametri</h3>

<dl>
 <dt>iterabile</dt>
 <dd>Se un <a href="/en-US/docs/Web/JavaScript/Reference/Statements/for...of">oggetto iterabile</a> è passato, tutti i suoi elementi saranno aggiunti al nuovo Set. null viene trattato come undefined.</dd>
</dl>

<h2 id="Descrizione">Descrizione</h2>

<p>Gli oggetti <code>Set</code> sono collezioni di valori, e possibile iterare i valori nel loro ordine di inserimento. Un valore in un <code>Set</code> può occorrere solo una volta; è quindi unico nella collezione.</p>

<h3 id="Uguaglianza_dei_valori">Uguaglianza dei valori</h3>

<p>Dato che ogni valore in un Set deve essere unico, dovra essere controllata l'uguaglianza di un nuovo valore con valori già presenti nel Set, questa operazione non è basata sullo stesso algoritmo usato per l'operatore ===. Nello specifico, per i Set, +0 (che è strettamente uguale a -0) e -0 sono valori differenti. Comunque, questo è stato cambiato nell'ultima specifica ECMAScript 6. Partendo da Gecko 29.0 {{geckoRelease("29")}} ({{bug("952870")}}) e da questa <a href="https://code.google.com/p/v8/issues/detail?id=3069">recente nightly Chrome issue</a>, +0 e -0 sono trattati come valori identici nell'oggetto <code>Set</code>. Inoltre, <code>NaN</code> e <code>undefined</code> possono essere memorizzati nei Set. <code>NaN</code> è considerato unguale a <code>NaN</code> (anche se <code>NaN</code> !== <code>NaN</code>).</p>

<h2 id="Proprietà">Proprietà</h2>

<dl>
 <dt><code>Set.size</code></dt>
 <dd>Il valore della proprietà <strong><font face="consolas, Liberation Mono, courier, monospace"><span style="background-color: rgba(220, 220, 220, 0.5);">size </span></font></strong>è 0. </dd>
 <dt>{{jsxref("Set.@@species", "get Set[@@species]")}}</dt>
 <dd>Il costruttore della funzione che viene usato per creare oggetti derivati.</dd>
 <dt>{{jsxref("Set.prototype")}}</dt>
 <dd>Rappresenta il prototipo per il costruttore del <code>Set</code>. Consente l'aggiunta di proprietà a tutti gli oggetti <code>Set</code>.</dd>
</dl>

<h2 id="Instanze_Set"><code>Instanze Set</code></h2>

<p>Tutte le instanze di <code>Set</code> ereditano da {{jsxref("Set.prototype")}}.</p>

<h3 id="Proprietà_2">Proprietà</h3>

<p>{{page('it-IT/Web/JavaScript/Reference/Global_Objects/Set/prototype','Properties')}}</p>

<h3 id="Methods">Methods</h3>

<p>{{page('it-IT/Web/JavaScript/Reference/Global_Objects/Set/prototype','Methods')}}</p>

<h2 id="Esempi">Esempi</h2>

<h3 id="Uso_dell'oggetto_Set">Uso dell'oggetto <code>Set</code></h3>

<pre class="brush: js">var mySet = new Set();

mySet.add(1);
mySet.add(5);
mySet.add("some text");
var o = {a: 1, b: 2};
mySet.add(o);

mySet.has(1); // true
mySet.has(3); // false, 3 non è stato aggiunto al set
mySet.has(5);              // true
mySet.has(Math.sqrt(25));  // true
mySet.has("Some Text".toLowerCase()); // true
mySet.has(o); // true

mySet.size; // 4

mySet.delete(5); // rimuove 5 dal set
mySet.has(5);    // false, 5 è stato rimosso

mySet.size; // 3, abbiamo rimosso 1 valore
</pre>

<h3 id="Iterando_oggetti_Set">Iterando oggetti Set</h3>

<pre class="brush: js">// iterando i valori in un set
// logga gli item in ordine: 1, "testo di esempio"
for (let item of mySet) console.log(item);

// logga gli item in ordine: 1, "testo di esempio"
for (let item of mySet.keys()) console.log(item);

// logga gli item in ordine: 1, "testo di esempio"
for (let item of mySet.values()) console.log(item);

// logga gli item in ordine: 1, "testo di esempio"
//(chiavi e valori qui sono uguali)
for (let [key, value] of mySet.entries()) console.log(key);

// converte un set in un Array semplice (con )
// convert set to plain Array (con <a href="/en-US/docs/Web/JavaScript/Reference/Operators/Array_comprehensions">Array comprehensions</a>)
var myArr = [v for (v of mySet)]; // [1, "some text"]
// Alternativa (con <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from">Array.from</a>)
var myArr = Array.from(mySet); // [1, "some text"]

// Il seguente snippet funzionerà anche in un documento HTML
mySet.add(document.body);
mySet.has(document.querySelector("body")); // true

// conversione tra Set e Array
mySet2 = new Set([1,2,3,4]);
mySet2.size; // 4
[...mySet2]; // [1,2,3,4]

// l'itersezione può essere simulata con
var intersection = new Set([...set1].filter(x =&gt; set2.has(x)));

// la differenza può essere simulata con
var difference = new Set([...set1].filter(x =&gt; !set2.has(x)));

// Itera i valori di un set con forEach
mySet.forEach(function(value) {
  console.log(value);
});

// 1
// 2
// 3
// 4</pre>

<h3 id="Relazione_con_gli_oggetti_Array">Relazione con gli oggetti <code>Array</code></h3>

<pre class="brush: js">var myArray = ["value1", "value2", "value3"];

// Uso del costruttore di Set per trasformare un Array in un Set
var mySet = new Set(myArray);

mySet.has("value1"); // ritorna true

// Usa l'operatore spread per trasformare un Set in un Array
console.log(uneval([...mySet])); // Mostrerà lo stesso identico Array di myArray</pre>

<h2 id="Specifica">Specifica</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-set-objects', 'Set')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>Initial definition.</td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-set-objects', 'Set')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</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>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>
    <p>{{ CompatChrome(38) }} [1]</p>
   </td>
   <td>{{ CompatGeckoDesktop("13") }}</td>
   <td>{{ CompatIE("11") }}</td>
   <td>25</td>
   <td>7.1</td>
  </tr>
  <tr>
   <td>Constructor argument: <code>new Set(iterable)</code></td>
   <td>{{ CompatChrome(38) }}</td>
   <td>{{ CompatGeckoDesktop("13") }}</td>
   <td>{{CompatNo}}</td>
   <td>25</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td>iterable</td>
   <td>{{ CompatChrome(38) }}</td>
   <td>{{ CompatGeckoDesktop("17") }}</td>
   <td>{{CompatNo}}</td>
   <td>25</td>
   <td>7.1</td>
  </tr>
  <tr>
   <td><code>Set.clear()</code></td>
   <td>{{ CompatChrome(38) }}</td>
   <td>{{CompatGeckoDesktop("19")}}</td>
   <td>{{ CompatIE("11") }}</td>
   <td>25</td>
   <td>7.1</td>
  </tr>
  <tr>
   <td><code>Set.keys(), Set.values(), Set.entries()</code></td>
   <td>{{ CompatChrome(38) }}</td>
   <td>{{CompatGeckoDesktop("24")}}</td>
   <td>{{CompatNo}}</td>
   <td>25</td>
   <td>7.1</td>
  </tr>
  <tr>
   <td><code>Set.forEach()</code></td>
   <td>{{ CompatChrome(38) }}</td>
   <td>{{CompatGeckoDesktop("25")}}</td>
   <td>{{ CompatIE("11") }}</td>
   <td>25</td>
   <td>7.1</td>
  </tr>
  <tr>
   <td>Value equality for -0 and 0</td>
   <td>{{ CompatChrome(38) }}</td>
   <td>{{CompatGeckoDesktop("29")}}</td>
   <td>{{CompatNo}}</td>
   <td>25</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td>Constructor argument: <code>new Set(null)</code></td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoDesktop("37")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td>Monkey-patched <code>add()</code> in Constructor</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoDesktop("37")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>Set[@@species]</code></td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoDesktop("41")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>Set()</code> without <code>new</code> throws</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoDesktop("42")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</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>{{CompatNo}}</td>
   <td>{{CompatChrome(38)}} [1]</td>
   <td>{{ CompatGeckoMobile("13") }}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>8</td>
  </tr>
  <tr>
   <td>Constructor argument: <code>new Set(iterable)</code></td>
   <td>{{CompatNo}}</td>
   <td>{{CompatChrome(38)}}</td>
   <td>{{ CompatGeckoMobile("13") }}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td>iterable</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{ CompatGeckoMobile("17") }}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>8</td>
  </tr>
  <tr>
   <td><code>Set.clear()</code></td>
   <td>{{CompatNo}}</td>
   <td>{{ CompatChrome(38) }}</td>
   <td>{{CompatGeckoMobile("19")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>8</td>
  </tr>
  <tr>
   <td><code>Set.keys(), Set.values(), Set.entries()</code></td>
   <td>{{CompatNo}}</td>
   <td>{{ CompatChrome(38) }}</td>
   <td>{{CompatGeckoMobile("24")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>8</td>
  </tr>
  <tr>
   <td><code>Set.forEach()</code></td>
   <td>{{CompatNo}}</td>
   <td>{{ CompatChrome(38) }}</td>
   <td>{{CompatGeckoMobile("25")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>8</td>
  </tr>
  <tr>
   <td>Value equality for -0 and 0</td>
   <td>{{CompatNo}}</td>
   <td>{{ CompatChrome(38) }}</td>
   <td>{{CompatGeckoMobile("29")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td>Constructor argument: <code>new Set(null)</code></td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoMobile("37")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td>Monkey-patched <code>add()</code> in Constructor</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoMobile("37")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>Set[@@species]</code></td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoMobile("41")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>Set()</code> without <code>new</code> throws</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoMobile("42")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<p>[1] La caratteristica è disponibile come opzione da Chrome 31. In <code>chrome://flags</code>, attivare la voce “Enable Experimental JavaScript”.</p>

<h2 id="Guarda_pure">Guarda pure</h2>

<ul>
 <li>{{jsxref("Map")}}</li>
 <li>{{jsxref("WeakMap")}}</li>
 <li>{{jsxref("WeakSet")}}</li>
</ul>