diff options
| author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:41:15 -0500 |
|---|---|---|
| committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:41:15 -0500 |
| commit | 4b1a9203c547c019fc5398082ae19a3f3d4c3efe (patch) | |
| tree | d4a40e13ceeb9f85479605110a76e7a4d5f3b56b /files/de/web/javascript/reference/global_objects/set | |
| parent | 33058f2b292b3a581333bdfb21b8f671898c5060 (diff) | |
| download | translated-content-4b1a9203c547c019fc5398082ae19a3f3d4c3efe.tar.gz translated-content-4b1a9203c547c019fc5398082ae19a3f3d4c3efe.tar.bz2 translated-content-4b1a9203c547c019fc5398082ae19a3f3d4c3efe.zip | |
initial commit
Diffstat (limited to 'files/de/web/javascript/reference/global_objects/set')
4 files changed, 548 insertions, 0 deletions
diff --git a/files/de/web/javascript/reference/global_objects/set/add/index.html b/files/de/web/javascript/reference/global_objects/set/add/index.html new file mode 100644 index 0000000000..1df111c1c5 --- /dev/null +++ b/files/de/web/javascript/reference/global_objects/set/add/index.html @@ -0,0 +1,69 @@ +--- +title: Set.prototype.add() +slug: Web/JavaScript/Reference/Global_Objects/Set/add +translation_of: Web/JavaScript/Reference/Global_Objects/Set/add +--- +<div>{{JSRef}}</div> + +<p>Die <strong><code>add()</code></strong> Methode fügt ein neues Element mit dem angegebenen Wert an das Ende eines <code>Set</code> Objekts hinzu.</p> + +<div>{{EmbedInteractiveExample("pages/js/set-prototype-add.html")}}</div> + +<div class="hidden">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 <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> and send us a pull request.</div> + +<h2 id="Syntax">Syntax</h2> + +<pre class="syntaxbox notranslate"><var>mySet</var>.add(<var>value</var>);</pre> + +<h3 id="Parameter">Parameter</h3> + +<dl> + <dt><code><var>value</var></code></dt> + <dd>Der hinzufügende Wert zu dem <code>Set</code> Objekt.</dd> +</dl> + +<h3 id="Rückgabewert">Rückgabewert</h3> + +<p>Das <code>Set</code> Objekt.</p> + +<h2 id="Beispiele">Beispiele</h2> + +<h3 id="Verwenden_der_add_Methode">Verwenden der add() Methode</h3> + +<pre class="brush: js notranslate">var mySet = new Set(); + +mySet.add(1); +mySet.add(5).add('some text'); // chainable + +console.log(mySet); +// Set [1, 5, "some text"] +</pre> + +<h2 id="Spezifikationen">Spezifikationen</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">Spezifikation</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('ESDraft', '#sec-set.prototype.add', 'Set.prototype.add')}}</td> + </tr> + </tbody> +</table> + +<h2 id="Browserkompatibilität">Browserkompatibilität</h2> + + + +<p>{{Compat("javascript.builtins.Set.add")}}</p> + +<h2 id="Siehe_auch">Siehe auch</h2> + +<ul> + <li>{{jsxref("Set")}}</li> + <li>{{jsxref("Set.prototype.delete()")}}</li> + <li>{{jsxref("Set.prototype.has()")}}</li> +</ul> diff --git a/files/de/web/javascript/reference/global_objects/set/delete/index.html b/files/de/web/javascript/reference/global_objects/set/delete/index.html new file mode 100644 index 0000000000..f0f4cf5f3e --- /dev/null +++ b/files/de/web/javascript/reference/global_objects/set/delete/index.html @@ -0,0 +1,84 @@ +--- +title: Set.prototype.delete() +slug: Web/JavaScript/Reference/Global_Objects/Set/delete +translation_of: Web/JavaScript/Reference/Global_Objects/Set/delete +--- +<div>{{JSRef}}</div> + +<p>Die <strong><code>delete()</code></strong> Methode entfernt das angegebene Element aus einem <code>Set</code> Objekt.</p> + +<div>{{EmbedInteractiveExample("pages/js/set-prototype-delete.html")}}</div> + +<div class="hidden">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 <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> and send us a pull request.</div> + +<h2 id="Syntax">Syntax</h2> + +<pre class="syntaxbox notranslate"><var>mySet</var>.delete(<var>value</var>);</pre> + +<h3 id="Parameter">Parameter</h3> + +<dl> + <dt><code><var>value</var></code></dt> + <dd>Der zu entfernende Wert aus <code><var>mySet</var></code>.</dd> +</dl> + +<h3 id="Rückgabewert">Rückgabewert</h3> + +<p>Rückgabewert ist <code>true</code> wenn <code><var>value</var></code> erfolgreich aus <code><var>mySet entfernt wurde</var></code>; ansonsten<code>false</code>.</p> + +<h2 id="Beispiel">Beispiel</h2> + +<h3 id="Verwenden_der_delete_Methode">Verwenden der delete() Methode</h3> + +<pre class="brush: js notranslate">const mySet = new Set(); +mySet.add('foo'); + +mySet.delete('bar'); // Returns false. No "bar" element found to be deleted. +mySet.delete('foo'); // Returns true. Successfully removed. + +mySet.has('foo'); // Returns false. The "foo" element is no longer present. +</pre> + +<p>Ein Beispiel, wie ein Objekt aus einem Set entfernt werden kann.</p> + +<pre class="brush: js notranslate">const setObj = new Set(); // Create a New Set. + +setObj.add({x: 10, y: 20}); // Add object in the set. + +setObj.add({x: 20, y: 30}); // Add object in the set. + +// Delete any point with `x > 10`. +setObj.forEach(function(point){ + if (point.x > 10){ + setObj.delete(point) + } +}) +</pre> + +<h2 id="Spezifikationen">Spezifikationen</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">Specification</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('ESDraft', '#sec-set.prototype.delete', 'Set.prototype.delete')}}</td> + </tr> + </tbody> +</table> + +<h2 id="Browserkompatibilität">Browserkompatibilität</h2> + + + +<p>{{Compat("javascript.builtins.Set.delete")}}</p> + +<h2 id="Siehe_auch">Siehe auch</h2> + +<ul> + <li>{{jsxref("Set")}}</li> + <li>{{jsxref("Set.prototype.clear()")}}</li> +</ul> diff --git a/files/de/web/javascript/reference/global_objects/set/has/index.html b/files/de/web/javascript/reference/global_objects/set/has/index.html new file mode 100644 index 0000000000..6374d80bc6 --- /dev/null +++ b/files/de/web/javascript/reference/global_objects/set/has/index.html @@ -0,0 +1,131 @@ +--- +title: Set.prototype.has() +slug: Web/JavaScript/Reference/Global_Objects/Set/has +tags: + - ECMAScript 2015 + - JavaScript + - Method + - Prototype + - set +translation_of: Web/JavaScript/Reference/Global_Objects/Set/has +--- +<div>{{JSRef}}</div> + +<p>Die Methode <code><strong>has()</strong></code> prüft, ob ein <code>Set</code>-Objekt ein Element mit dem angegebenen Wert enthält und gibt entsprechend <code>true</code> oder <code>false</code> zurück.</p> + +<h2 id="Syntax">Syntax</h2> + +<pre class="syntaxbox"><code><em>mySet</em>.has(value);</code></pre> + +<h3 id="Parameter">Parameter</h3> + +<dl> + <dt>value</dt> + <dd>Der zu suchende Wert.</dd> +</dl> + +<h3 id="Rückgabewert">Rückgabewert</h3> + +<dl> + <dt>Boolean</dt> + <dd>Gibt <code>true</code> zurück, falls ein Element mit dem angegebenen Wert innerhalb des <code>Set</code>-Objektes existiert, ansonsten wird <code>false</code> zurückgegeben.</dd> +</dl> + +<h2 id="Beispiel">Beispiel</h2> + +<h3 id="has()_verwenden"><code>has()</code> verwenden</h3> + +<p>Das folgende Beispiel verwendet <code>has()</code>, um zu prüfen, ob bestimmte Werte in einem <code>Set</code> enthalten sind oder nicht.</p> + +<pre class="brush: js">var mySet = new Set(); +mySet.add('foo'); + +mySet.has('foo'); // returns true +mySet.has('bar'); // returns false +</pre> + +<h2 id="Spezifikationen">Spezifikationen</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Spezifikation</th> + <th scope="col">Status</th> + <th scope="col">Kommentar</th> + </tr> + <tr> + <td>{{SpecName('ES2015', '#sec-set.prototype.has', 'Set.prototype.has')}}</td> + <td>{{Spec2('ES2015')}}</td> + <td>Initiale Definition.</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-set.prototype.has', 'Set.prototype.has')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="Browserkompatibilität">Browserkompatibilität</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>38</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoDesktop("13.0")}}</td> + <td>11</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>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>38</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoMobile("13.0")}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>8</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="Siehe_auch">Siehe auch</h2> + +<ul> + <li>{{jsxref("Set")}}</li> + <li>{{jsxref("Set.prototype.add()")}}</li> + <li>{{jsxref("Set.prototype.delete()")}}</li> +</ul> diff --git a/files/de/web/javascript/reference/global_objects/set/index.html b/files/de/web/javascript/reference/global_objects/set/index.html new file mode 100644 index 0000000000..27a3b1ad3d --- /dev/null +++ b/files/de/web/javascript/reference/global_objects/set/index.html @@ -0,0 +1,264 @@ +--- +title: Set +slug: Web/JavaScript/Reference/Global_Objects/Set +tags: + - ECMAScript6 + - Global Objects + - JavaScript + - Referenz + - set +translation_of: Web/JavaScript/Reference/Global_Objects/Set +--- +<div>{{JSRef}}</div> + +<p>Ein <strong><code>Set</code></strong>-Objekt speichert <em>eindeutige</em> Werte jedes beliebigen Typs, egal ob es sich dabei um {{Glossary("Primitive", "primitive Werte")}} oder Objektreferenzen handelt.</p> + +<div>{{EmbedInteractiveExample("pages/js/set-prototype-constructor.html")}}</div> + + + +<h2 id="Syntax">Syntax</h2> + +<pre class="syntaxbox notranslate">new Set([iterable]);</pre> + +<h3 id="Parameter">Parameter</h3> + +<dl> + <dt>iterable</dt> + <dd>Ein <a href="/de/docs/Web/JavaScript/Reference/Statements/for...of">iterierbares Objekt</a>, dessen Elemente zum neuen <code>Set</code> hinzugefügt werden sollen.<br> + Ist dieser Parameter nicht angegeben oder <code>null</code>, wird ein leeres <code>Set</code> erzeugt.</dd> +</dl> + +<h3 id="Rückgabewert">Rückgabewert</h3> + +<p>Ein neues <code>Set</code> Objekt.</p> + +<h2 id="Beschreibung">Beschreibung</h2> + +<p><code>Set</code> Objekte sind Sammlungen von Werten. Man kann über die Elemente iterieren; sie werden in der Reihenfolge ihres Hinzufügens gespeichert. Jeder Wert ist <strong>nur einmal vorhanden</strong>; er ist im <code>Set</code> eindeutig.</p> + +<h3 id="Gleichheit">Gleichheit</h3> + +<p>Da jeder Wert innerhalb eines <code>Set</code>s eindeutig sein muss, werden alle Werte auf Gleichheit überprüft. In einer früheren Version der ECMAScript Spezifikation verhielt sich der Algorithmus dafür anders als der für den <code>===</code> Operator. Insbesondere waren für <code>Set</code>s <code>+0</code> und <code>-0</code> verschiedene Werte (obwohl sie streng gleich sind). In der ECMAScript 2015 Spezifikation wurde das geändert, so dass <code>+0</code> und <code>-0</code> jetzt jeweils als gleicher Wert gelten. Siehe auch den Eintrag "Key equality for -0 and 0" in der Übersicht zur <a href="#browserkompatibilität">Browserkompatibilität</a>.</p> + +<p>{{jsxref("NaN")}} und {{jsxref("undefined")}} können ebenfalls in einem Set gespeichert werden. Dabei werden alle <code>NaN</code> Werte gleichgesetzt, d.h. <code>NaN</code> gilt gleich <code>NaN</code> (auch wenn <code>NaN !== NaN</code>).</p> + +<h2 id="Eigenschaften">Eigenschaften</h2> + +<dl> + <dt><code>Set.length</code></dt> + <dd>Der Wert der Eigenschaft <code>length</code> ist immer 0.<br> + Die Anzahl der Elemente eines Sets befindet sich in {{jsxref("Set.prototype.size")}}.</dd> + <dt>{{jsxref("Set.@@species", "get Set[@@species]")}}</dt> + <dd>Die Konstruktorfunktion, um abgeleitete Objekte zu erzeugen.</dd> + <dt>{{jsxref("Set.prototype")}}</dt> + <dd>Prototyp für den <code>Set</code> Konstruktor. Ermöglicht das Hinzufügen von Eigenschaften zu allen <code>Set</code> Objekten.</dd> +</dl> + +<h2 id="Set_Instanzen"><code>Set</code> Instanzen</h2> + +<p>Alle <code>Set</code> Instanzen erben von {{jsxref("Set.prototype")}}.</p> + +<h3 id="Eigenschaften_2">Eigenschaften</h3> + +<p>{{page('en-US/Web/JavaScript/Reference/Global_Objects/Set/prototype','Properties')}}</p> + +<h3 id="Methoden">Methoden</h3> + +<p>{{page('en-US/Web/JavaScript/Reference/Global_Objects/Set/prototype','Methods')}}</p> + +<h2 id="Beispiele">Beispiele</h2> + +<h3 id="Verwenden_eines_Set_Objekts">Verwenden eines <code>Set</code> Objekts</h3> + +<pre class="brush: js notranslate">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 has not been added to the 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); // removes 5 from the set +mySet.has(5); // false, 5 has been removed + +mySet.size; // 3, we just removed one value +</pre> + +<h3 id="Iterieren_über_ein_Set">Iterieren über ein <code>Set</code></h3> + +<pre class="brush: js notranslate">// iterate over items in set +// logs the items in the order: 1, "some text" +for (let item of mySet) console.log(item); + +// logs the items in the order: 1, "some text" +for (let item of mySet.keys()) console.log(item); + +// logs the items in the order: 1, "some text" +for (let item of mySet.values()) console.log(item); + +// logs the items in the order: 1, "some text" +//(key and value are the same here) +for (let [key, value] of mySet.entries()) console.log(key); + +// convert set to plain Array +var myArr = Array.from(mySet); // [1, "some text"] + +// the following will also work if run in an HTML document +mySet.add(document.body); +mySet.has(document.querySelector("body")); // true + +// converting between Set and Array +mySet2 = new Set([1,2,3,4]); +mySet2.size; // 4 +[...mySet2]; // [1,2,3,4] + +// intersect can be simulated via +var intersection = new Set([...set1].filter(x => set2.has(x))); + +// difference can be simulated via +var difference = new Set([...set1].filter(x => !set2.has(x))); + +// Iterate set entries with forEach +mySet.forEach(function(value) { + console.log(value); +}); + +// 1 +// 2 +// 3 +// 4</pre> + +<h3 id="Gebräuchliche_Set_Operationen_implementieren">Gebräuchliche <code>Set</code> Operationen implementieren</h3> + +<pre class="brush: js line-numbers language-js notranslate"><code class="language-js"><span class="keyword token">function</span> <span class="function token">isSuperset</span><span class="punctuation token">(</span><span class="parameter token"><span class="keyword token">set</span><span class="punctuation token">,</span> subset</span><span class="punctuation token">)</span> <span class="punctuation token">{</span> + <span class="keyword token">for</span> <span class="punctuation token">(</span><span class="keyword token">var</span> elem <span class="keyword token">of</span> subset<span class="punctuation token">)</span> <span class="punctuation token">{</span> + <span class="keyword token">if</span> <span class="punctuation token">(</span><span class="operator token">!</span><span class="keyword token">set</span><span class="punctuation token">.</span><span class="function token">has</span><span class="punctuation token">(</span>elem<span class="punctuation token">)</span><span class="punctuation token">)</span> <span class="punctuation token">{</span> + <span class="keyword token">return</span> <span class="boolean token">false</span><span class="punctuation token">;</span> + <span class="punctuation token">}</span> + <span class="punctuation token">}</span> + <span class="keyword token">return</span> <span class="boolean token">true</span><span class="punctuation token">;</span> +<span class="punctuation token">}</span> + +<span class="keyword token">function</span> <span class="function token">union</span><span class="punctuation token">(</span><span class="parameter token">setA<span class="punctuation token">,</span> setB</span><span class="punctuation token">)</span> <span class="punctuation token">{</span> + <span class="keyword token">var</span> _union <span class="operator token">=</span> <span class="keyword token">new</span> <span class="class-name token">Set</span><span class="punctuation token">(</span>setA<span class="punctuation token">)</span><span class="punctuation token">;</span> + <span class="keyword token">for</span> <span class="punctuation token">(</span><span class="keyword token">var</span> elem <span class="keyword token">of</span> setB<span class="punctuation token">)</span> <span class="punctuation token">{</span> + _union<span class="punctuation token">.</span><span class="function token">add</span><span class="punctuation token">(</span>elem<span class="punctuation token">)</span><span class="punctuation token">;</span> + <span class="punctuation token">}</span> + <span class="keyword token">return</span> _union<span class="punctuation token">;</span> +<span class="punctuation token">}</span> + +<span class="keyword token">function</span> <span class="function token">intersection</span><span class="punctuation token">(</span><span class="parameter token">setA<span class="punctuation token">,</span> setB</span><span class="punctuation token">)</span> <span class="punctuation token">{</span> + <span class="keyword token">var</span> _intersection <span class="operator token">=</span> <span class="keyword token">new</span> <span class="class-name token">Set</span><span class="punctuation token">(</span><span class="punctuation token">)</span><span class="punctuation token">;</span> + <span class="keyword token">for</span> <span class="punctuation token">(</span><span class="keyword token">var</span> elem <span class="keyword token">of</span> setB<span class="punctuation token">)</span> <span class="punctuation token">{</span> + <span class="keyword token">if</span> <span class="punctuation token">(</span>setA<span class="punctuation token">.</span><span class="function token">has</span><span class="punctuation token">(</span>elem<span class="punctuation token">)</span><span class="punctuation token">)</span> <span class="punctuation token">{</span> + _intersection<span class="punctuation token">.</span><span class="function token">add</span><span class="punctuation token">(</span>elem<span class="punctuation token">)</span><span class="punctuation token">;</span> + <span class="punctuation token">}</span> + <span class="punctuation token">}</span> + <span class="keyword token">return</span> _intersection<span class="punctuation token">;</span> +<span class="punctuation token">}</span> + +<span class="keyword token">function</span> <span class="function token">symmetricDifference</span><span class="punctuation token">(</span><span class="parameter token">setA<span class="punctuation token">,</span> setB</span><span class="punctuation token">)</span> <span class="punctuation token">{</span> + <span class="keyword token">var</span> _difference <span class="operator token">=</span> <span class="keyword token">new</span> <span class="class-name token">Set</span><span class="punctuation token">(</span>setA<span class="punctuation token">)</span><span class="punctuation token">;</span> + <span class="keyword token">for</span> <span class="punctuation token">(</span><span class="keyword token">var</span> elem <span class="keyword token">of</span> setB<span class="punctuation token">)</span> <span class="punctuation token">{</span> + <span class="keyword token">if</span> <span class="punctuation token">(</span>_difference<span class="punctuation token">.</span><span class="function token">has</span><span class="punctuation token">(</span>elem<span class="punctuation token">)</span><span class="punctuation token">)</span> <span class="punctuation token">{</span> + _difference<span class="punctuation token">.</span><span class="function token">delete</span><span class="punctuation token">(</span>elem<span class="punctuation token">)</span><span class="punctuation token">;</span> + <span class="punctuation token">}</span> <span class="keyword token">else</span> <span class="punctuation token">{</span> + _difference<span class="punctuation token">.</span><span class="function token">add</span><span class="punctuation token">(</span>elem<span class="punctuation token">)</span><span class="punctuation token">;</span> + <span class="punctuation token">}</span> + <span class="punctuation token">}</span> + <span class="keyword token">return</span> _difference<span class="punctuation token">;</span> +<span class="punctuation token">}</span> + +<span class="keyword token">function</span> <span class="function token">difference</span><span class="punctuation token">(</span><span class="parameter token">setA<span class="punctuation token">,</span> setB</span><span class="punctuation token">)</span> <span class="punctuation token">{</span> + <span class="keyword token">var</span> _difference <span class="operator token">=</span> <span class="keyword token">new</span> <span class="class-name token">Set</span><span class="punctuation token">(</span>setA<span class="punctuation token">)</span><span class="punctuation token">;</span> + <span class="keyword token">for</span> <span class="punctuation token">(</span><span class="keyword token">var</span> elem <span class="keyword token">of</span> setB<span class="punctuation token">)</span> <span class="punctuation token">{</span> + _difference<span class="punctuation token">.</span><span class="function token">delete</span><span class="punctuation token">(</span>elem<span class="punctuation token">)</span><span class="punctuation token">;</span> + <span class="punctuation token">}</span> + <span class="keyword token">return</span> _difference<span class="punctuation token">;</span> +<span class="punctuation token">}</span> + +<span class="comment token">//Examples</span> +<span class="keyword token">var</span> setA <span class="operator token">=</span> <span class="keyword token">new</span> <span class="class-name token">Set</span><span class="punctuation token">(</span><span class="punctuation token">[</span><span class="number token">1</span><span class="punctuation token">,</span> <span class="number token">2</span><span class="punctuation token">,</span> <span class="number token">3</span><span class="punctuation token">,</span> <span class="number token">4</span><span class="punctuation token">]</span><span class="punctuation token">)</span><span class="punctuation token">,</span> + setB <span class="operator token">=</span> <span class="keyword token">new</span> <span class="class-name token">Set</span><span class="punctuation token">(</span><span class="punctuation token">[</span><span class="number token">2</span><span class="punctuation token">,</span> <span class="number token">3</span><span class="punctuation token">]</span><span class="punctuation token">)</span><span class="punctuation token">,</span> + setC <span class="operator token">=</span> <span class="keyword token">new</span> <span class="class-name token">Set</span><span class="punctuation token">(</span><span class="punctuation token">[</span><span class="number token">3</span><span class="punctuation token">,</span> <span class="number token">4</span><span class="punctuation token">,</span> <span class="number token">5</span><span class="punctuation token">,</span> <span class="number token">6</span><span class="punctuation token">]</span><span class="punctuation token">)</span><span class="punctuation token">;</span> + +<span class="function token">isSuperset</span><span class="punctuation token">(</span>setA<span class="punctuation token">,</span> setB<span class="punctuation token">)</span><span class="punctuation token">;</span> <span class="comment token">// => true</span> +<span class="function token">union</span><span class="punctuation token">(</span>setA<span class="punctuation token">,</span> setC<span class="punctuation token">)</span><span class="punctuation token">;</span> <span class="comment token">// => Set [1, 2, 3, 4, 5, 6]</span> +<span class="function token">intersection</span><span class="punctuation token">(</span>setA<span class="punctuation token">,</span> setC<span class="punctuation token">)</span><span class="punctuation token">;</span> <span class="comment token">// => Set [3, 4]</span> +<span class="function token">symmetricDifference</span><span class="punctuation token">(</span>setA<span class="punctuation token">,</span> setC<span class="punctuation token">)</span><span class="punctuation token">;</span> <span class="comment token">// => Set [1, 2, 5, 6]</span> +<span class="function token">difference</span><span class="punctuation token">(</span>setA<span class="punctuation token">,</span> setC<span class="punctuation token">)</span><span class="punctuation token">;</span> <span class="comment token">// => Set [1, 2]</span></code></pre> + +<h3 id="Array_und_Set"><code>Array</code> und <code>Set</code></h3> + +<pre class="brush: js notranslate">var myArray = ["value1", "value2", "value3"]; + +// Use the regular Set constructor to transform an Array into a Set +var mySet = new Set(myArray); + +mySet.has("value1"); // returns true + +// Use the spread operator to transform a set into an Array. +console.log([...mySet]); // Will show you exactly the same Array as myArray</pre> + +<h3 id="Duplikate_entfernen_aus_einem_Array">Duplikate entfernen aus einem <code>Array</code></h3> + +<pre class="brush: js line-numbers language-js notranslate"><code class="language-js"><span class="comment token">// Use to remove duplicate elements from the array </span> + +<span class="keyword token">const</span> numbers <span class="operator token">=</span> <span class="punctuation token">[</span><span class="number token">2</span><span class="punctuation token">,</span><span class="number token">3</span><span class="punctuation token">,</span><span class="number token">4</span><span class="punctuation token">,</span><span class="number token">4</span><span class="punctuation token">,</span><span class="number token">2</span><span class="punctuation token">,</span><span class="number token">3</span><span class="punctuation token">,</span><span class="number token">3</span><span class="punctuation token">,</span><span class="number token">4</span><span class="punctuation token">,</span><span class="number token">4</span><span class="punctuation token">,</span><span class="number token">5</span><span class="punctuation token">,</span><span class="number token">5</span><span class="punctuation token">,</span><span class="number token">6</span><span class="punctuation token">,</span><span class="number token">6</span><span class="punctuation token">,</span><span class="number token">7</span><span class="punctuation token">,</span><span class="number token">5</span><span class="punctuation token">,</span><span class="number token">32</span><span class="punctuation token">,</span><span class="number token">3</span><span class="punctuation token">,</span><span class="number token">4</span><span class="punctuation token">,</span><span class="number token">5</span><span class="punctuation token">]</span> + +console<span class="punctuation token">.</span><span class="function token">log</span><span class="punctuation token">(</span><span class="punctuation token">[</span><span class="operator token">...</span>new <span class="class-name token">Set</span><span class="punctuation token">(</span>numbers<span class="punctuation token">)</span><span class="punctuation token">]</span><span class="punctuation token">)</span> + +<span class="comment token">// [2, 3, 4, 5, 6, 7, 32]</span></code></pre> + +<h3 id="String_und_Set"><code>String</code> und <code>Set</code></h3> + +<pre class="brush: js line-numbers language-js notranslate"><code class="language-js"><span class="keyword token">var</span> text <span class="operator token">=</span> <span class="string token">'India'</span><span class="punctuation token">;</span> + +<span class="keyword token">var</span> mySet <span class="operator token">=</span> <span class="keyword token">new</span> <span class="class-name token">Set</span><span class="punctuation token">(</span>text<span class="punctuation token">)</span><span class="punctuation token">;</span> <span class="comment token">// Set ['I', 'n', 'd', 'i', 'a']</span> +mySet<span class="punctuation token">.</span>size<span class="punctuation token">;</span> <span class="comment token">// 5</span></code></pre> + +<h2 id="Spezifikationen">Spezifikationen</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Spezifikation</th> + <th scope="col">Status</th> + <th scope="col">Kommentar</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="Browserkompatibilität"><a id="browserkompatibilität" name="browserkompatibilität"></a>Browserkompatibilität</h2> + + + +<p>{{Compat("javascript.builtins.Set")}}</p> + +<h2 id="Siehe_auch">Siehe auch</h2> + +<ul> + <li>{{jsxref("Map")}}</li> + <li>{{jsxref("WeakMap")}}</li> + <li>{{jsxref("WeakSet")}}</li> +</ul> |
