Die includes() Methode prüft, ob ein Array ein bestimmtes Element enthält und gibt entsprechend true oder false aus.
Syntax
varboolean=array.includes(searchElement[, fromIndex])
Rückgabewert
Ein Boolean.
Parameter
searchElement- Das zu suchende Element.
fromIndex- Optional. DIe Position im Array ab welcher die Suche nach
searchElementbeginnt. Ein negativer Wert fängt beim Indexarray.length + fromIndexan zu suchen. Default ist 0.
Beispiele
[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
Polyfill
if (!Array.prototype.includes) {
Array.prototype.includes = function(searchElement /*, fromIndex*/) {
'use strict';
if (this == null) {
throw new TypeError('Array.prototype.includes called on null or undefined');
}
var O = Object(this);
var len = parseInt(O.length, 10) || 0;
if (len === 0) {
return false;
}
var n = parseInt(arguments[1], 10) || 0;
var k;
if (n >= 0) {
k = n;
} else {
k = len + n;
if (k < 0) {k = 0;}
}
var currentElement;
while (k < len) {
currentElement = O[k];
if (searchElement === currentElement ||
(searchElement !== searchElement && currentElement !== currentElement)) { // NaN !== NaN
return true;
}
k++;
}
return false;
};
}
Spezifikationen
| Spezifikation | Status | Kommentar |
|---|---|---|
| ECMAScript 2016 (ECMA-262) Die Definition von 'Array.prototype.includes' in dieser Spezifikation. |
Standard | Initiale Definition. |
| ECMAScript 2017 Draft (ECMA-262) Die Definition von 'Array.prototype.includes' in dieser Spezifikation. |
Entwurf |
Browserkompatibilität
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | 47 | 43 | Nicht unterstützt | 34 | 9 |
| Feature | Android | Android Webview | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Android |
|---|---|---|---|---|---|---|---|
| Basic support | Nicht unterstützt | 47 | 43 | Nicht unterstützt | 34 | 9 | 47 |
Siehe auch
Schlagwörter des Dokuments und Mitwirkende
Schlagwörter:
Mitwirkende an dieser Seite:
schlagi123,
adabru
Zuletzt aktualisiert von:
schlagi123,