aboutsummaryrefslogtreecommitdiff
path: root/files/pl/web/javascript/reference/global_objects/set/index.html
blob: 995d238f970e28d78fcb952ca5a9b8d61cb80b6f (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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
---
title: Set
slug: Web/JavaScript/Reference/Global_Objects/Set
tags:
  - ECMAScript 2015
  - ECMAScript6
  - Global Objects
  - JavaScript
  - Object
  - set
translation_of: Web/JavaScript/Reference/Global_Objects/Set
original_slug: Web/JavaScript/Referencje/Obiekty/Set
---
<div>{{JSRef}}</div>

<p>Obiekt <code><strong>Set</strong></code> umożliwia przechowywanie <em>unikalnych </em>wartości każdego typu, zarówno {{Glossary("Primitive", "primitywów")}} jak i obiektów.</p>

<h2 id="Składnia">Składnia</h2>

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

<h3 id="Parametry">Parametry</h3>

<dl>
 <dt><code>iterable</code></dt>
 <dd>Jeżeli przekażesz <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of">obiekt iterowalny</a>, wszystkie jego elementy zostaną dodane do nowego <code>Set</code>. Podczas gdy nie przekażemy żadnego parametru lub wartość parametru będzie równa <em><code>null</code></em>, zostanie stworzony pusty <code>Set</code>.</dd>
</dl>

<h3 id="Zwracana_wartość">Zwracana wartość</h3>

<p>Nowy obiekt <code>Set</code>.</p>

<h2 id="Opis">Opis</h2>

<p>Obiekt <code><em>Set</em></code> jest kolekcją wartości. Możesz iterować po elementach <code><em>Set</em></code> w kolejności, w której zostały dodane. Wartość w <code><em>Set</em></code> <strong>może występować tylko jeden raz</strong>.</p>

<h3 id="Równość_wartości">Równość wartości</h3>

<p>Dlatego, że każda wartość w <code>Set </code>musi być unikalna, musi zostać to sprawdzone. We wcześniejszych specyfikacjach ECMAScript nie było to oparte na tym samym algorytmie co w przypadku operatora ===. Konkretnie dla Set +0 (co jest tym samym co -0) i -0 były innymi wartościami. W specyfikacji ECMAScript 2015 zostało to zmienione. Zobacz "Value equality for -0 and 0" w tabeli <a href="https://developer.mozilla.org/pl/docs/Web/JavaScript/Referencje/Obiekty/Set#Kompatybilność_przeglądarek">Kompatybilność z przeglądarkami</a>.</p>

<p><code>NaN</code> i <code>undefined</code> mogą być przechowywane w <code>Set</code>. <code>NaN</code> w <code>Set</code> uważane jest za równe <code>NaN</code>, podczas gdy <code>NaN !== NaN</code> zwraca <code>true</code></p>

<h2 id="Własności">Własności</h2>

<dl>
 <dt><code>Set.length</code></dt>
 <dd>Wartość <code>length</code> zawsze wynosi 0.</dd>
 <dt>{{jsxref("Set.@@species", "get Set[@@species]")}}</dt>
 <dd>Funkcja wykorzystywana do stworzenia pochodnych obiektów.</dd>
 <dt>{{jsxref("Set.prototype")}}</dt>
 <dd>Reprezentuje prototyp konstruktora <code>Set.</code> Pozwala na dodanie własności do obiektu <code>Set</code>.</dd>
</dl>

<h2 id="Instancje_Set"><font face="consolas, Liberation Mono, courier, monospace">Instancje <code>Set</code></font></h2>

<p>Wszystkie instancje <code>Set</code> dziedziczą od {{jsxref("Set.prototype")}}.</p>

<h3 id="Własności_2">Własności</h3>

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

<h3 id="Metody">Metody</h3>

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

<h2 id="Przykłady">Przykłady</h2>

<h3 id="Użycie_obiektu_Set">Użycie obiektu <code>Set</code></h3>

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

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

mySet.add({a: 1, b: 2}); // o jest referencją do innego obiektu, więc dwa obiekty zostają dodane do Set.

mySet.has(1); // true
mySet.has(3); // false, 3 nie zostało dodane do Set.
mySet.has(5);              // true
mySet.has(Math.sqrt(25));  // true
mySet.has('Some Text'.toLowerCase()); // true
mySet.has(o); // true

mySet.size; // 5

mySet.delete(5); // Usuwa 5 z Set.
mySet.has(5);    // false, 5 zostało usunięte.

mySet.size; // 4, usuneliśmy jedną wartość.
console.log(mySet);// Set {1, "some text", Object {a: 1, b: 2}, Object {a: 1, b: 2}}</pre>

<h3 id="Iterowanie_po_Set.">Iterowanie po Set.</h3>

<pre class="brush: js">// Iterowanie po items w Set.
// wypisuje items w kolejności: 1, "some text", {"a": 1, "b": 2}
for (let item of mySet) console.log(item);

// wypisuje items w kolejności: 1, "some text", {"a": 1, "b": 2}
for (let item of mySet.keys()) console.log(item);

// wypisuje items w kolejności: 1, "some text", {"a": 1, "b": 2}
for (let item of mySet.values()) console.log(item);

// wypisuje items w kolejności: 1, "some text", {"a": 1, "b": 2}
//(key i value są takie same)
for (let [key, value] of mySet.entries()) console.log(key);

// zamienia Set na Array, przy użyciu Array.from
var myArr = Array.from(mySet); // [1, "some text", {"a": 1, "b": 2}]

// następujące funkcje również zadziałają, jeżeli skrypt odpalony jest w dokumencie HTML
mySet.add(document.body);
mySet.has(document.querySelector('body')); // true

// zamiana Array na Set i na odwrót
mySet2 = new Set([1, 2, 3, 4]);
mySet2.size; // 4
[...mySet2]; // [1, 2, 3, 4]

// Set z wartościami, które są w set1 i set2, może być uzyskany następująco
var intersection = new Set([...set1].filter(x =&gt; set2.has(x)));

// Set z różnicami wartości set1 i set2 może być uzyskany następująco
var difference = new Set([...set1].filter(x =&gt; !set2.has(x)));

// iterowanie po Set za pomocą .forEach
mySet.forEach(function(value) {
  console.log(value);
});
// 1
// 2
// 3
// 4</pre>

<h3 id="Implementacja_podstawowych_operacji_Set">Implementacja podstawowych operacji <code>Set</code></h3>

<pre class="brush: js">Set.prototype.isSuperset = function(subset) {
    for (var elem of subset) {
        if (!this.has(elem)) {
            return false;
        }
    }
    return true;
}

Set.prototype.union = function(setB) {
    var union = new Set(this);
    for (var elem of setB) {
        union.add(elem);
    }
    return union;
}

Set.prototype.intersection = function(setB) {
    var intersection = new Set();
    for (var elem of setB) {
        if (this.has(elem)) {
            intersection.add(elem);
        }
    }
    return intersection;
}

Set.prototype.difference = function(setB) {
    var difference = new Set(this);
    for (var elem of setB) {
        difference.delete(elem);
    }
    return difference;
}

// Przykłady
var setA = new Set([1, 2, 3, 4]),
    setB = new Set([2, 3]),
    setC = new Set([3, 4, 5, 6]);

setA.isSuperset(setB); // =&gt; true
setA.union(setC); // =&gt; Set [1, 2, 3, 4, 5, 6]
setA.intersection(setC); // =&gt; Set [3, 4]
setA.difference(setC); // =&gt; Set [1, 2]

</pre>

<h3 id="Relacje_z_Tablicami_(Array)">Relacje z Tablicami (<code>Array</code>)</h3>

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

// Użycie konstruktora Set do zamiany Array na Set.
var mySet = new Set(myArray);

mySet.has('value1'); // =&gt; true

// Użycie spread operator do zamiany Set na Array.
console.log([...mySet]); // Will show you exactly the same Array as myArray</pre>

<h2 id="Specyfikacje">Specyfikacje</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specyfikacja</th>
   <th scope="col">Status</th>
   <th scope="col">Komentarz</th>
  </tr>
  <tr>
   <td>{{SpecName('ES2015', '#sec-set-objects', 'Set')}}</td>
   <td>{{Spec2('ES2015')}}</td>
   <td>Początkowa definicja.</td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-set-objects', 'Set')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="Kompatybilność_przeglądarek">Kompatybilność przeglądarek</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Edge</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>12</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>12</td>
   <td>{{ CompatGeckoDesktop("13") }}</td>
   <td>{{CompatNo}}</td>
   <td>25</td>
   <td>9.0</td>
  </tr>
  <tr>
   <td>iterable</td>
   <td>{{ CompatChrome(38) }}</td>
   <td>12</td>
   <td>{{ CompatGeckoDesktop("17") }}</td>
   <td>{{CompatNo}}</td>
   <td>25</td>
   <td>7.1</td>
  </tr>
  <tr>
   <td><code>Set.add()</code> returns the set</td>
   <td>{{ CompatChrome(38) }}</td>
   <td>12</td>
   <td>{{ CompatGeckoDesktop("13") }}</td>
   <td>{{CompatNo}}</td>
   <td>25</td>
   <td>7.1</td>
  </tr>
  <tr>
   <td><code>Set.clear()</code></td>
   <td>{{ CompatChrome(38) }}</td>
   <td>12</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>12</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>12</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>12</td>
   <td>{{CompatGeckoDesktop("29")}}</td>
   <td>{{CompatNo}}</td>
   <td>25</td>
   <td>{{CompatSafari(9)}}</td>
  </tr>
  <tr>
   <td>Constructor argument: <code>new Set(null)</code></td>
   <td>{{CompatVersionUnknown}}</td>
   <td>12</td>
   <td>{{CompatGeckoDesktop("37")}}</td>
   <td>{{CompatIE(11)}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatSafari(7.1)}}</td>
  </tr>
  <tr>
   <td>Monkey-patched <code>add()</code> in Constructor</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>12</td>
   <td>{{CompatGeckoDesktop("37")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatSafari(9)}}</td>
  </tr>
  <tr>
   <td><code>Set[@@species]</code></td>
   <td>{{ CompatChrome(51) }}</td>
   <td>13</td>
   <td>{{CompatGeckoDesktop("41")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{ CompatOpera(38) }}</td>
   <td>{{CompatSafari(10)}}</td>
  </tr>
  <tr>
   <td><code>Set()</code> without <code>new</code> throws</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>12</td>
   <td>{{CompatGeckoDesktop("42")}}</td>
   <td>{{CompatIE(11)}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>9</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>Edge</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>{{CompatVersionUnknown}}</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>{{CompatVersionUnknown}}</td>
   <td>{{ CompatGeckoMobile("13") }}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>9</td>
  </tr>
  <tr>
   <td>iterable</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatVersionUnknown}}</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>{{CompatVersionUnknown}}</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>{{CompatVersionUnknown}}</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>{{CompatVersionUnknown}}</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>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoMobile("29")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>9</td>
  </tr>
  <tr>
   <td>Constructor argument: <code>new Set(null)</code></td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoMobile("37")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>8</td>
  </tr>
  <tr>
   <td>Monkey-patched <code>add()</code> in Constructor</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoMobile("37")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>9</td>
  </tr>
  <tr>
   <td><code>Set[@@species]</code></td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoMobile("41")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>10</td>
  </tr>
  <tr>
   <td><code>Set()</code> without <code>new</code> throws</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoMobile("42")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>9</td>
  </tr>
 </tbody>
</table>
</div>

<p>[1] The feature was available behind a preference from Chrome 31. In <code>chrome://flags</code>, activate the entry “Enable Experimental JavaScript”.</p>

<h2 id="Zobacz_również">Zobacz również</h2>

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