Why does typeof array with objects return “Object” and not “Array”?

Posted chucklu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Why does typeof array with objects return “Object” and not “Array”?相关的知识,希望对你有一定的参考价值。

https://stackoverflow.com/questions/4775722/check-if-object-is-an-array

One of the weird behaviour and spec in javascript is the typeof Array is Object.

You can check if the variable is an array in couple of ways:

var isArr = data instanceof Array;
var isArr = Array.isArray(data);

But the most reliable way is:

isArr = Object.prototype.toString.call(data) == ‘[object Array]‘;

Since you tagged your question with jQuery, you can use jQuery isArray function:

var isArr = $.isArray(data);

 

以上是关于Why does typeof array with objects return “Object” and not “Array”?的主要内容,如果未能解决你的问题,请参考以下文章