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
|
---
title: Array.prototype.includes()
slug: Web/JavaScript/Reference/Global_Objects/Array/includes
translation_of: Web/JavaScript/Reference/Global_Objects/Array/includes
---
<div>{{JSRef}}</div>
<p>De methode <code><strong>includes()</strong></code> controleert of de opgegeven waarde aanwezig is in een reeks of niet. Als dit waar is geeft het <code>true</code>, anders <code>false</code>.</p>
<pre class="brush: js">var a = [1, 2, 3];
a.includes(2); // true
a.includes(4); // false</pre>
<h2 id="Syntax">Syntax</h2>
<pre><var>arr</var>.includes(<var>zoekWaarde[</var>, <var>vanIndex]</var>)</pre>
<h3 id="Parameters">Parameters</h3>
<dl>
<dt><code>zoekWaarde</code></dt>
<dd>Het element om te zoeken.</dd>
<dt><code>vanIndex</code> {{optional_inline}}</dt>
<dd>De positie binnen de array waar begonnen wordt met het zoeken naar <code>zoekWaarde</code>. Een negatieve waarde zoekt oplopend uit de index van array.length + vanIndex. Standaard 0.</dd>
</dl>
<h3 id="Antwoord_waarde">Antwoord waarde</h3>
<p>Een {{jsxref("Boolean")}}. <code>true</code> als de <code>zoekWaarde</code> is gevonden. <code>false</code> als dit niet het geval is. de 0 (nul) worden als gelijk gezien. -0 is gelijk aan 0 en +0. <code>false</code> staat niet gelijk aan 0</p>
<p><font face="x-locale-heading-primary, zillaslab, Palatino, Palatino Linotype, x-locale-heading-secondary, serif"><span style="font-size: 40px;"><strong>Voorbeelden</strong></span></font></p>
<p> </p>
<pre class="brush: js">[1, 2, 3].includes(2); // true <em>(waar)</em>
[1, 2, 3].includes(4); // false <em>(niet waar)</em>
[1, 2, 3].includes(3, 3); // false <em>(niet waar)</em>
[1, 2, 3].includes(3, -1); // true <em>(waar)</em>
[1, 2, NaN].includes(NaN); // true <em>(waar) </em>(NaN betekent "Not A Number" oftewel "geen nummer" in het Engels)
</pre>
<p> </p>
<h3 id="fromIndex_is_groter_dan_of_gelijk_aan_de_array_lengte"><code>fromIndex</code> is groter dan of gelijk aan de array lengte</h3>
<p>Als <code>fromIndex</code> groter of gelijk is aan de lengte van de array, wordt er <code>false</code> geantwoord. De array zal niet doorzocht worden.</p>
<pre class="brush: js">var arr = ['a', 'b', 'c'];
arr.includes('c', 3); // false <em>(niet waar)</em>
arr.includes('c', 100); // false <em>(niet waar)</em></pre>
<h3 id="De_berekende_index_is_minder_dan_0">De berekende index is minder dan 0</h3>
<p>Als <font face="consolas, Liberation Mono, courier, monospace"><span style="background-color: rgba(220, 220, 220, 0.498039);">vanIndex</span></font> negatief is, zal de berekende index worden berekend om te worden gebruikt als een positie in de array waarop moet worden gezocht naar <code>zoekElement</code>. Als de berekende index lager is dan 0, wordt de hele array doorzocht.</p>
<pre class="brush: js">// array lengte is 3
// vanIndex is -100
// berekende index is 3 + (-100) = -97
var arr = ['a', 'b', 'c'];
arr.includes('a', -100); // true <em>(waar)</em>
arr.includes('b', -100); // true <em>(waar)</em>
arr.includes('c', -100); // true <em>(waar)</em></pre>
<h3 id="includes()_gebruiken_als_een_algemene_methode"><code>includes()</code> gebruiken als een algemene methode</h3>
<p>De <code>includes()</code> methode is natuurlijk algemeen. Het is niet nodig dat <code>deze</code> waarde een Array is. Het onderstaande voorbeeld laat de <code>includes()</code> methode zien in een functie's <a href="/en-US/docs/Web/JavaScript/Reference/Functions/arguments">argumenten</a> lijst. </p>
<pre class="brush: js">(function() {
console.log([].includes.call(arguments, 'a')); // true <em>(waar)</em>
console.log([].includes.call(arguments, 'd')); // false <em>(niet waar)</em>
})('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) {
// 1. Let O be ? ToObject(this value).
if (this == null) {
throw new TypeError('"this" is null or not defined');
}
var o = Object(this);
// 2. Let len be ? ToLength(? Get(O, "length")).
var len = o.length >>> 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 < 0,
// a. Let k be len + n.
// b. If k < 0, let k be 0.
var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
// 7. Repeat, while k < len
while (k < len) {
// a. Let elementK be the result of ? Get(O, ! ToString(k)).
// b. If SameValueZero(searchElement, elementK) is true, return true.
// c. Increase k by 1.
// NOTE: === provides the correct "SameValueZero" comparison needed here.
if (o[k] === searchElement) {
return true;
}
k++;
}
// 8. Return false
return false;
}
});
}
</pre>
<p>If you need to support truly obsolete JavaScript engines that don't support <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty">Object.defineProperty</a></code>, it's best not to polyfill <code>Array.prototype</code> methods at all, as you can't make them non-enumerable.</p>
<h2 id="Specifications">Specifications</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('ES7', '#sec-array.prototype.includes', 'Array.prototype.includes')}}</td>
<td>{{Spec2('ES7')}}</td>
<td>Initial 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="Browser_compatibility">Browser compatibility</h2>
<div>{{CompatibilityTable}}</div>
<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>Edge</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>Basic support</td>
<td>
<p>{{CompatChrome(47)}}</p>
</td>
<td>{{CompatGeckoDesktop("43")}}</td>
<td>{{CompatNo}}</td>
<td>14</td>
<td>34</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>Android Webview</th>
<th>Firefox Mobile (Gecko)</th>
<th>IE Mobile</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
<th>Chrome for Android</th>
</tr>
<tr>
<td>Basic support</td>
<td>{{CompatNo}}</td>
<td>
<p>{{CompatChrome(47)}}</p>
</td>
<td>{{CompatGeckoMobile("43")}}</td>
<td>{{CompatNo}}</td>
<td>34</td>
<td>9</td>
<td>
<p>{{CompatChrome(47)}}</p>
</td>
</tr>
</tbody>
</table>
</div>
<h2 id="See_also">See also</h2>
<ul>
<li>{{jsxref("TypedArray.prototype.includes()")}}</li>
<li>{{jsxref("String.prototype.includes()")}}</li>
<li>{{jsxref("Array.prototype.indexOf()")}}</li>
</ul>
|