Array.isArray()
El mètode Array.isArray()
retorna true
si un objecte és un array, o false
en cas que no ho sigui.
Sintaxi
Array.isArray(obj)
Paràmetres
obj
- L'objecte que s'ha de comprovar.
Descripció
Vegeu l'article “Determinar amb absoluta precisió si un objecte JavaScript és un array o no” per més detalls.
Exemples
// totes les crides següents retornen true
Array.isArray([]);
Array.isArray([1]);
Array.isArray(new Array());
// Fet poc conegut: Array.prototype és un array per si mateix:
Array.isArray(Array.prototype);
// totes les crides següents retornen 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({ __proto__: Array.prototype });
Polyfill
Executar el codi següent abans de cap altre codi crearà Array.isArray()
si no es troba disponible de forma nativa.
if (!Array.isArray) {
Array.isArray = function(arg) {
return Object.prototype.toString.call(arg) === '[object Array]';
};
}
Especificacions
Especificació | Estat | Comentaris |
---|---|---|
ECMAScript 5.1 (ECMA-262) The definition of 'Array.isArray' in that specification. |
Standard | Definició inicial. Implementat en JavaScript 1.8.5. |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Array.isArray' in that specification. |
Standard |
Compatibilitat amb navegadors
We're converting our compatibility data into a machine-readable JSON format.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
Find out how you can help! (en-US)
Característica | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Suport bàsic | 5 | 4.0 (2.0) | 9 | 10.5 | 5 |
Característica | Android | Chrome per Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Suport bàsic | (Yes) | (Yes) | 4.0 (2.0) | (Yes) | (Yes) | (Yes) |