Baseline Widely available
Die Array.isArray()
statische Methode bestimmt, ob der übergebene Wert ein Array
ist.
console.log(Array.isArray([1, 3, 5]));
// Expected output: true
console.log(Array.isArray("[]"));
// Expected output: false
console.log(Array.isArray(new Array(5)));
// Expected output: true
console.log(Array.isArray(new Int16Array([15, 33])));
// Expected output: false
Syntax Parameter
value
Der zu prüfende Wert.
true
, wenn value
ein Array
ist; andernfalls false
. false
wird immer zurückgegeben, wenn value
eine Instanz von TypedArray
ist.
Array.isArray()
prüft, ob der übergebene Wert ein Array
ist. Es führt eine markierte Prüfung durch, ähnlich dem in
Operator, für ein privates Feld, das durch den Array()
Konstruktor initialisiert wird.
Es ist eine robustere Alternative zum instanceof Array
, da es falsche Positive und falsche Negative vermeidet:
Array.isArray()
lehnt Werte ab, die keine tatsächlichen Array
Instanzen sind, selbst wenn sie Array.prototype
in ihrer Prototypenkette haben â instanceof Array
würde diese akzeptieren, da es die Prototypenkette überprüft.Array.isArray()
akzeptiert Array
Objekte, die in einem anderen Kontext konstruiert wurden â instanceof Array
gibt false
für diese zurück, da die Identität des Array
Konstruktors zwischen den Kontexten verschieden ist.Details finden Sie im Artikel "Determining with absolute accuracy whether or not a JavaScript object is an array".
Beispiele Verwendung von Array.isArray()// all following calls return true
Array.isArray([]);
Array.isArray([1]);
Array.isArray(new Array());
Array.isArray(new Array("a", "b", "c", "d"));
Array.isArray(new Array(3));
// Little known fact: Array.prototype itself is an array:
Array.isArray(Array.prototype);
// all following calls return false
Array.isArray();
Array.isArray({});
Array.isArray(null);
Array.isArray(undefined);
Array.isArray(17);
Array.isArray("Array");
Array.isArray(true);
Array.isArray(false);
Array.isArray(new Uint8Array(32));
// This is not an array, because it was not created using the
// array literal syntax or the Array constructor
Array.isArray({ __proto__: Array.prototype });
instanceof vs. Array.isArray()
Beim Prüfen auf eine Array
Instanz wird Array.isArray()
dem instanceof
vorgezogen, da es über verschiedene Kontexte hinweg funktioniert.
const iframe = document.createElement("iframe");
document.body.appendChild(iframe);
const xArray = window.frames[window.frames.length - 1].Array;
const arr = new xArray(1, 2, 3); // [1, 2, 3]
// Correctly checking for Array
Array.isArray(arr); // true
// The prototype of arr is xArray.prototype, which is a
// different object from Array.prototype
arr instanceof Array; // false
Spezifikationen Browser-Kompatibilität Siehe auch
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4