aboutsummaryrefslogtreecommitdiff
path: root/files/de/web/javascript/reference/global_objects/array/includes/index.html
blob: 64a51dfb730e59d6e9a2228ffcbb4ee4b9387f71 (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
---
title: Array.prototype.includes()
slug: Web/JavaScript/Reference/Global_Objects/Array/includes
tags:
  - Array
  - JavaScript
  - Method
  - Prototype
  - Reference
  - polyfill
translation_of: Web/JavaScript/Reference/Global_Objects/Array/includes
---
<div>{{JSRef}}</div>

<p>Die <code><strong>includes()</strong></code> Methode prüft, ob ein Array ein bestimmtes Element enthält, und gibt entsprechend <code>true</code> oder <code>false</code> aus. Es wird der selbe sameValueZero-Algorithmus benutzt, um ein gegebenes Element zu finden.</p>

<div>{{EmbedInteractiveExample("pages/js/array-includes.html")}}</div>



<h2 id="Syntax">Syntax</h2>

<pre class="syntaxbox"><code><var>arr</var>.includes(<var>searchElement</var>[, <var>fromIndex</var>])</code></pre>

<h3 id="Parameter">Parameter</h3>

<dl>
 <dt><code>searchElement</code></dt>
 <dd>Das zu suchende Element.</dd>
 <dt><code>fromIndex </code> {{optional_inline}}</dt>
 <dd>Die Position im Array, ab welcher die Suche nach <code>searchElement</code> beginnt. Bei einem negativen Wert fängt die Suche beim Index <code>array.length - fromIndex</code> an. Default ist 0.</dd>
</dl>

<h3 id="Rückgabewert">Rückgabewert</h3>

<dl>
 <dt>
 <p>Ein {{jsxref("Boolean")}}.</p>
 </dt>
</dl>

<h2 id="Beispiele">Beispiele</h2>

<pre class="brush: js">[1, 2, 3].includes(2);     // true
[1, 2, 3].includes(4);     // false
[1, 2, 3].includes(3, 3);  // false
[1, 2, 3].includes(3, -1); // true
[1, 2, NaN].includes(NaN); // true
</pre>

<h3 id="fromIndex_ist_größer_oder_gleich_der_Arraylänge"><code>fromIndex</code> ist größer oder gleich der Arraylänge</h3>

<p>Wenn <code>fromIndex</code> größer oder gleich der Arraylänge ist, wird <code>false</code> zurückgegeben. Das Array wird nicht durchsucht.</p>

<pre class="brush: js">var arr = ['a', 'b', 'c'];

arr.includes('c', 3);   // false
arr.includes('c', 100); // false
</pre>

<h3 id="Berechneter_Index_ist_kleiner_als_0">Berechneter Index ist kleiner als 0</h3>

<p>Wenn <code>fromIndex</code> negativ ist, wird der Index berechnet, an dem die Suche im Array nach <code>searchElement</code> beginnen soll. Wenn diese Berechnung einen Index kleiner als 0 ergibt, wird das ganze Array durchsucht.</p>

<pre class="brush: js">// Arraylänge ist 3
// fromIndex ist -100
// Der berechnete Index ist 3 + (-100) = -97

var arr = ['a', 'b', 'c'];

arr.includes('a', -100); // true
arr.includes('b', -100); // true
arr.includes('c', -100); // true
</pre>

<h3 id="Einsatz_von_includes()_als_generische_Methode">Einsatz von <code>includes()</code> als generische Methode</h3>

<p>Die <code>includes()</code>-Methode is absichtlich generisch. Die <code>this</code>-Referenz muss nicht auf ein Array-Objekt zeigen, so dass auch andere Objekte (z. B. Array-ähnliche Objekte) genutzt werden können. Das Beispiel zeigt, wie <code>includes()</code> auf den Parametern (<a href="/de/docs/Web/JavaScript/Reference/Functions/arguments">arguments</a>) einer Funktion aufgerufen wird.</p>

<pre class="brush: js">(function() {
  console.log([].includes.call(arguments, 'a')); // true
  console.log([].includes.call(arguments, 'd')); // false
})('a', 'b', 'c')
</pre>

<h2 id="Polyfill">Polyfill</h2>

<pre class="brush: js">// https://tc39.github.io/ecma262/#sec-array.prototype.includes
if (!Array.prototype.includes) {
  Object.defineProperty(Array.prototype, 'includes', {
    value: function(searchElement, fromIndex) {

      if (this == null) {
        throw new TypeError('"this" is null or not defined');
      }

      // 1. Let O be ? ToObject(this value).
      var o = Object(this);

      // 2. Let len be ? ToLength(? Get(O, "length")).
      var len = o.length &gt;&gt;&gt; 0;

      // 3. If len is 0, return false.
      if (len === 0) {
        return false;
      }

      // 4. Let n be ? ToInteger(fromIndex).
      //    (If fromIndex is undefined, this step produces the value 0.)
      var n = fromIndex | 0;

      // 5. If n ≥ 0, then
      //  a. Let k be n.
      // 6. Else n &lt; 0,
      //  a. Let k be len + n.
      //  b. If k &lt; 0, let k be 0.
      var k = Math.max(n &gt;= 0 ? n : len - Math.abs(n), 0);

      function sameValueZero(x, y) {
        return x === y || (typeof x === 'number' &amp;&amp; typeof y === 'number' &amp;&amp; isNaN(x) &amp;&amp; isNaN(y));
      }

      // 7. Repeat, while k &lt; len
      while (k &lt; len) {
        // a. Let elementK be the result of ? Get(O, ! ToString(k)).
        // b. If SameValueZero(searchElement, elementK) is true, return true.
        if (sameValueZero(o[k], searchElement)) {
          return true;
        }
        // c. Increase k by 1.
        k++;
      }

      // 8. Return false
      return false;
    }
  });
}
</pre>

<p>Sollten Sie wirklich veraltete JavaScript-Engines unterstützen müssen, die ihrerseits {{jsxref("Object.defineProperty", "Object.defineProperty")}} nicht unterstützen, ist es ratsam, die <code>Array.prototype</code>-Methode nicht mit dem Polyfill zu erweitern, da man diese nicht unabzählbar (non-enumerable) machen kann.</p>

<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('ES7', '#sec-array.prototype.includes', 'Array.prototype.includes')}}</td>
   <td>{{Spec2('ES7')}}</td>
   <td>Initiale Definition.</td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-array.prototype.includes', 'Array.prototype.includes')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td></td>
  </tr>
 </tbody>
</table>

<h2 id="Browserkompatibilität">Browserkompatibilität</h2>

<div>


<p>{{Compat("javascript.builtins.Array.includes")}}</p>
</div>

<h2 id="Siehe_auch">Siehe auch</h2>

<ul>
 <li>{{jsxref("TypedArray.prototype.includes()")}}</li>
 <li>{{jsxref("String.prototype.includes()")}}</li>
 <li>{{jsxref("Array.prototype.indexOf()")}}</li>
 <li>{{jsxref("Array.prototype.find()")}}</li>
 <li>{{jsxref("Array.prototype.findIndex()")}}</li>
</ul>