admin管理员组文章数量:1134247
How should I detect if the argument is an array because typeof []
returns 'object'
and I want to distinguish between arrays and objects.
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.
JSON.parse
and JSON.stringify
are able to make this distinction. How can I do it?
I am using Node.JS which is based on V8 the same as Chrome.
How should I detect if the argument is an array because typeof []
returns 'object'
and I want to distinguish between arrays and objects.
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.
JSON.parse
and JSON.stringify
are able to make this distinction. How can I do it?
I am using Node.JS which is based on V8 the same as Chrome.
Share Improve this question asked May 9, 2011 at 19:46 700 Software700 Software 87.7k87 gold badges242 silver badges347 bronze badges5 Answers
Reset to default 172Array.isArray
native V8 function. It's fast, it's always correct. This is part of ES5.
arr instanceof Array
Checks whether the object was made with the array constructor.
_.isArray // underscore method.
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.
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.
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.
Try this code:
Array.isArray(argument)
How about:
your_object instanceof Array
In V8 in Chrome I get
[] instanceof Array
> true
({}) instanceof Array
> false
({"0":"string","1":"string","length":"2"}) instanceof Array
> false
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.
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.
Try this way:
console.log(Object.prototype.toString.call(arg).replace(/^[object (.+)]$/, '$1').toLowerCase())
本文标签: JavaScript detect if argument is array instead of object (NodeJS)Stack Overflow
版权声明:本文标题:JavaScript: detect if argument is array instead of object (Node.JS) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736841312a1955112.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论