如何检测一个变量是一个数组 [英] How to detect if a variable is an array
问题描述
什么是最好的事实标准跨浏览器的方法来确定在JavaScript中的变量是一个数组或不?
What is the best de-facto standard cross-browser method to determine if a variable in JavaScript is an array or not?
在搜索网络有许多不同的建议,一些好和相当多的无效
Searching the web there are a number of different suggestions, some good and quite a few invalid.
例如,下面是一个基本的方法:
For example, the following is a basic approach:
function isArray(obj) {
return (obj && obj.length);
}
但是,请注意,如果数组是空的会发生什么,或者OBJ其实不是一个数组,但实现了一个length属性等。
However, note what happens if the array is empty, or obj actually is not an array but implements a length property, etc.
因此,这是执行最好在实际工作,是跨浏览器的,仍然有效地执行?术语
So which implementation is the best in terms of actually working, being cross-browser and still perform efficiently?
推荐答案
在JS对象的类型检查,通过做的instanceof
,即
Type checking of objects in JS is done via instanceof
, ie
obj instanceof Array
如果对象跨越帧边界作为传递每一帧都有自己的阵列
对象这是行不通的。您可以通过检查解决这个内部的 [等级] 的对象的属性。为了得到它,使用 Object.prototype.toString()
(这是保证由ECMA-262工作):
This won't work if the object is passed across frame boundaries as each frame has its own Array
object. You can work around this by checking the internal [[Class]] property of the object. To get it, use Object.prototype.toString()
(this is guaranteed to work by ECMA-262):
Object.prototype.toString.call(obj) === '[object Array]'
这两种方法都将只对实际阵列和阵列没有状物体,如参数
对象或节点列表工作。由于所有阵列状物体必须有一个数字长度
属性,我会检查这些是这样的:
Both methods will only work for actual arrays and not array-like objects like the arguments
object or node lists. As all array-like objects must have a numeric length
property, I'd check for these like this:
typeof obj !== 'undefined' && obj !== null && typeof obj.length === 'number'
请注意,字符串将通过此检查,这可能会导致问题,因为IE浏览器不允许使用索引访问字符串的字符。因此,您可能需要更改 typeof运算的obj!= =未定义
到 typeof运算的obj ===对象
排除原语和主机对象有不同的类型,从对象
产品总数。这将仍然让字符串对象通过,这将必须手动排除。
Please note that strings will pass this check, which might lead to problems as IE doesn't allow access to a string's characters by index. Therefore, you might want to change typeof obj !== 'undefined'
to typeof obj === 'object'
to exclude primitives and host objects with types distinct from 'object'
alltogether. This will still let string objects pass, which would have to be excluded manually.
在大多数情况下,你居然想知道的是你是否可以通过数字通过索引对象进行迭代。因此,它可能是一个好主意,检查对象 0
不是命名的属性,可以通过这些检查之一来进行:
In most cases, what you actually want to know is whether you can iterate over the object via numeric indices. Therefore, it might be a good idea to check if the object has a property named 0
instead, which can be done via one of these checks:
typeof obj[0] !== 'undefined' // false negative for `obj[0] = undefined`
obj.hasOwnProperty('0') // exclude array-likes with inherited entries
'0' in Object(obj) // include array-likes with inherited entries
演员阵容到对象需要为阵列状的原语(即字符串)正常工作。
The cast to object is necessary to work correctly for array-like primitives (ie strings).
这里的code鲁棒检查JS数组:
Here's the code for robust checks for JS arrays:
function isArray(obj) {
return Object.prototype.toString.call(obj) === '[object Array]';
}
和迭代(即非空)阵列状物体:
and iterable (ie non-empty) array-like objects:
function isNonEmptyArrayLike(obj) {
try { // don't bother with `typeof` - just access `length` and `catch`
return obj.length > 0 && '0' in Object(obj);
}
catch(e) {
return false;
}
}
这篇关于如何检测一个变量是一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!