diff options
Diffstat (limited to 'files/nl/web/javascript/reference/global_objects/array/includes/index.html')
| -rw-r--r-- | files/nl/web/javascript/reference/global_objects/array/includes/index.html | 220 |
1 files changed, 220 insertions, 0 deletions
diff --git a/files/nl/web/javascript/reference/global_objects/array/includes/index.html b/files/nl/web/javascript/reference/global_objects/array/includes/index.html new file mode 100644 index 0000000000..104c9a7705 --- /dev/null +++ b/files/nl/web/javascript/reference/global_objects/array/includes/index.html @@ -0,0 +1,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> |
