JavaScript:检测参数是否是数组而不是对象(Node.JS)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5941706/
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
JavaScript: detect if argument is array instead of object (Node.JS)
提问by Bryan Field
How should I detect if the argument is an array because typeof []
returns 'object'
and I want to distinguish between arrays and objects.
我应该如何检测参数是否是数组,因为typeof []
返回'object'
并且我想区分数组和对象。
It is possible that object will look like {"0":"string","1":"string","length":"2"}
but I don't want it to come out as an array if it is in fact an object looking like an array.
对象可能看起来像,{"0":"string","1":"string","length":"2"}
但如果它实际上是一个看起来像数组的对象,我不希望它以数组的形式出现。
JSON.parse
and JSON.stringify
are able to make this distinction. How can I do it?
JSON.parse
并且JSON.stringify
能够做出这种区分。我该怎么做?
I am using Node.JS which is based on V8 the same as Chrome.
我使用的是基于 V8 的 Node.JS,与 Chrome 相同。
回答by Raynos
Array.isArray
Array.isArray
native V8 function. It's fast, it's always correct. This is part of ES5.
原生 V8 功能。它很快,总是正确的。这是 ES5 的一部分。
arr instanceof Array
arr instanceof Array
Checks whether the object was made with the array constructor.
检查对象是否是用数组构造函数创建的。
A method from underscore. Here is a snippet taken from the their source
来自下划线的方法。这是从他们的来源中摘取的片段
var toString = Object.prototype.toString,
nativeIsArray = Array.isArray;
_.isArray = nativeIsArray || function(obj) {
return toString.call(obj) === '[object Array]';
};
This method takes an object and calls the Object.prototype.toString
method on it. This will always return [object Array]
for arrays.
这个方法接受一个对象并调用Object.prototype.toString
它的方法。这将始终返回[object Array]
数组。
In my personal experience I find asking the toString
method is the most effective but it's not as short or readable as instanceof Array
nor is it as fast as Array.isArray
but that's ES5 code and I tend to avoid using it for portability.
根据我的个人经验,我发现询问该toString
方法是最有效的,但它不像ES5 代码那样简短或可读,instanceof Array
也不像Array.isArray
ES5 代码那样快,而且我倾向于避免使用它来实现可移植性。
I would personally recommend you try using underscore
, which is a library with common utility methods in it. It has a lot of useful functions that DRY up your code.
我个人建议您尝试使用underscore
,这是一个包含常用实用程序方法的库。它有很多有用的功能可以干掉你的代码。
回答by T-Bull
Try this code:
试试这个代码:
Array.isArray(argument)
回答by t.dubrownik
How about:
怎么样:
your_object instanceof Array
In V8 in Chrome I get
在 Chrome 的 V8 中,我得到
[] instanceof Array
> true
({}) instanceof Array
> false
({"0":"string","1":"string","length":"2"}) instanceof Array
> false
回答by Tom
I looks like this question has several good answers, but for completeness I would add another option, which have not been suggested earlier.
我看起来这个问题有几个很好的答案,但为了完整起见,我会添加另一个选项,之前没有建议过。
In order to check if something is an array, you can use Node.js util
native module and its isArray()
function.
为了检查某个东西是否是一个数组,您可以使用 Node.jsutil
原生模块及其isArray()
功能。
Example:
例子:
var util = require('util');
util.isArray([]); // true
util.isArray(new Array); // true
util.isArray({"0":"string","1":"string","length":"2"}); // false
With that method you do not have to worry about JS standards implemented by V8 as it will always show the right answer.
使用这种方法,您不必担心 V8 实现的 JS 标准,因为它始终会显示正确的答案。
回答by Hymany
Try this way:
console.log(Object.prototype.toString.call(arg).replace(/^[object (.+)]$/, '$1').toLowerCase())
试试这样:
console.log(Object.prototype.toString.call(arg).replace(/^[object (.+)]$/, '$1').toLowerCase())