检测传递的参数是否为数组?Javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2763024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Detect if parameter passed is an array? Javascript
提问by Alex
Possible Duplicate:
How to detect if a variable is an array
可能的重复:
如何检测变量是否为数组
I have a simple question:
我有一个简单的问题:
How do I detect if a parameter passed to my javascript function is an array? I don't believe that I can test:
如何检测传递给我的 javascript 函数的参数是否为数组?我不相信我可以测试:
if (typeof paramThatCouldBeArray == 'array')
So is it possible?
那么有可能吗?
How would I do it?
我该怎么做?
Thanks in advance.
提前致谢。
回答by Casey Chu
if (param instanceof Array)
...
Edit.As of 2016, there is a ready-built method that catches more corner cases, Array.isArray, used as follows:
编辑。截至 2016 年,有一个现成的方法可以捕获更多的极端情况Array.isArray,使用如下:
if (Array.isArray(param))
...
回答by James Westgate
This is the approach jQuery 1.4.2 uses:
这是 jQuery 1.4.2 使用的方法:
var toString = param.prototype.toString;
var isArray = function(obj) {
return toString.call(obj) === "[object Array]";
}
回答by Germán Rodríguez
回答by outis
You can test the constructorproperty:
您可以测试该constructor属性:
if (param.constructor == Array) {
...
}
Though this will include objects that have an array prototype,
虽然这将包括具有数组原型的对象,
function Stack() {
}
Stack.prototype = [];
unless they also define constructor:
除非他们还定义了构造函数:
Stack.prototype.constructor = Stack;
or:
或者:
function Stack() {
this.constructor = Stack;
}
回答by CMS
Some days agoI was building a simple type detectionfunction, maybe its useful for you:
一些天前我是建立一个简单的类型检测功能,也许其对您有用:
Usage:
用法:
//...
if (typeString(obj) == 'array') {
//..
}
Implementation:
执行:
function typeString(o) {
if (typeof o != 'object')
return typeof o;
if (o === null)
return "null";
//object, array, function, date, regexp, string, number, boolean, error
var internalClass = Object.prototype.toString.call(o)
.match(/\[object\s(\w+)\]/)[1];
return internalClass.toLowerCase();
}
The second variantof this function is more strict, because it returns only object types described in the ECMAScript specification (possible output values: "object", "undefined", "null", and "function", "array", "date", "regexp", "string", "number", "boolean""error", using the [[Class]]internal property).
此函数的第二个变体更为严格,因为它仅返回 ECMAScript 规范中描述的对象类型(可能的输出值:"object", "undefined", "null", and "function", "array", "date", "regexp", "string", "number", "boolean""error",使用[[Class]]内部属性)。
回答by Ionu? G. Stan
Duck Typying
鸭子打字
Actually, you don't necessarily want to check that an object is an array. You should duck typeit and the only thing you want that object to implement is the lengthproperty. After this you can transform it into an array:
实际上,您不一定要检查对象是否是数组。您应该输入它,并且您希望该对象实现的唯一内容是length属性。在此之后,您可以将其转换为数组:
var arrayLike = {
length : 3,
0: "foo"
};
// transform object to array
var array = Array.prototype.slice.call(arrayLike);
JSON.stringify(array); // ["foo", null, null]
Array.prototype.slice.call(object)will transform into an array every object that exposes a lengthproperty. In the case of strings for example:
Array.prototype.slice.call(object)将每个公开length属性的对象转换为数组。以字符串为例:
var array = Array.prototype.slice.call("123");
JSON.stringify(array); // ["1", "2", "3"]
Well, this technique it's not quite working on IE6 (I don't know about later versions), but it's easy to create a small utility function to transform objects in arrays.
嗯,这种技术在 IE6 上不太适用(我不知道以后的版本),但是很容易创建一个小的实用程序函数来转换数组中的对象。

