From 074785cea106179cb3305637055ab0a009ca74f2 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:42:52 -0500 Subject: initial commit --- .../global_objects/array/includes/index.html | 220 +++++++++++++++++++++ 1 file changed, 220 insertions(+) create mode 100644 files/nl/web/javascript/reference/global_objects/array/includes/index.html (limited to 'files/nl/web/javascript/reference/global_objects/array/includes/index.html') 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 +--- +
{{JSRef}}
+ +

De methode includes() controleert of de opgegeven waarde aanwezig is in een reeks of niet. Als dit waar is geeft het true, anders false.

+ +
var a = [1, 2, 3];
+a.includes(2); // true
+a.includes(4); // false
+ +

Syntax

+ +
arr.includes(zoekWaarde[vanIndex])
+ +

Parameters

+ +
+
zoekWaarde
+
Het element om te zoeken.
+
vanIndex {{optional_inline}}
+
De positie binnen de array waar begonnen wordt met het zoeken naar zoekWaarde. Een negatieve waarde zoekt oplopend uit de index van array.length + vanIndex. Standaard 0.
+
+ +

Antwoord waarde

+ +

Een {{jsxref("Boolean")}}. true als de zoekWaarde is gevonden. false als dit niet het geval is. de 0 (nul) worden als gelijk gezien. -0 is gelijk aan 0 en +0. false staat niet gelijk aan 0

+ +

Voorbeelden

+ +

 

+ +
[1, 2, 3].includes(2);     // true (waar)
+[1, 2, 3].includes(4);     // false (niet waar)
+[1, 2, 3].includes(3, 3);  // false (niet waar)
+[1, 2, 3].includes(3, -1); // true (waar)
+[1, 2, NaN].includes(NaN); // true (waar) (NaN betekent "Not A Number" oftewel "geen nummer" in het Engels)
+
+ +

 

+ +

fromIndex is groter dan of gelijk aan de array lengte

+ +

Als fromIndex groter of gelijk is aan de lengte van de array, wordt er false geantwoord.  De array zal niet doorzocht worden.

+ +
var arr = ['a', 'b', 'c'];
+
+arr.includes('c', 3);   // false (niet waar)
+arr.includes('c', 100); // false (niet waar)
+ +

De berekende index is minder dan 0

+ +

Als vanIndex negatief is, zal de berekende index worden berekend om te worden gebruikt als een positie in de array waarop moet worden gezocht naar zoekElement. Als de berekende index lager is dan 0, wordt de hele array doorzocht.

+ +
// array lengte is 3
+// vanIndex is -100
+// berekende index is 3 + (-100) = -97
+
+var arr = ['a', 'b', 'c'];
+
+arr.includes('a', -100); // true (waar)
+arr.includes('b', -100); // true (waar)
+arr.includes('c', -100); // true (waar)
+ +

includes() gebruiken als een algemene methode

+ +

De includes() methode is natuurlijk algemeen. Het is niet nodig dat deze waarde een Array is. Het onderstaande voorbeeld laat de includes() methode zien in een functie's argumenten lijst. 

+ +
(function() {
+  console.log([].includes.call(arguments, 'a')); // true (waar)
+  console.log([].includes.call(arguments, 'd')); // false (niet waar)
+})('a','b','c');
+ +

Polyfill

+ +
// 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;
+    }
+  });
+}
+
+ +

If you need to support truly obsolete JavaScript engines that don't support Object.defineProperty, it's best not to polyfill Array.prototype methods at all, as you can't make them non-enumerable.

+ +

Specifications

+ + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ES7', '#sec-array.prototype.includes', 'Array.prototype.includes')}}{{Spec2('ES7')}}Initial definition.
{{SpecName('ESDraft', '#sec-array.prototype.includes', 'Array.prototype.includes')}}{{Spec2('ESDraft')}} 
+ +

Browser compatibility

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerEdgeOperaSafari
Basic support +

{{CompatChrome(47)}}

+
{{CompatGeckoDesktop("43")}}{{CompatNo}}14349
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidAndroid WebviewFirefox Mobile (Gecko)IE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatNo}} +

{{CompatChrome(47)}}

+
{{CompatGeckoMobile("43")}}{{CompatNo}}349 +

{{CompatChrome(47)}}

+
+
+ +

See also

+ + -- cgit v1.2.3-54-g00ecf