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);

原文地址:https://www.cnblogs.com/chucklu/p/10259628.html

时间: 2024-08-23 02:57:01

Why does typeof array with objects return “Object” and not “Array”?的相关文章

php--------对象(object) 与 数组(array) 的转换

php开发中常常用到数组,sql数据都是数组,数组和对象用的也是比较多的,常常相互转化,数组是PHP的灵魂,非常强大,面向对象编程也是挺方便的. /** * 数组 转 对象 * * @param array $arr 数组 * @return object */ function array_to_object($arr) { if (gettype($arr) != 'array') { return; } foreach ($arr as $k => $v) { if (gettype($v

Array of Objects

You should be comfortable with the content in the modules up to and including the module "Arrays" for this project. Create a class called consultCo that holds a private class called employee that contains the name, pay rate and social security n

JavaScript基础——引用类型(一)Object类型、Array类型

引用类型简介 引用类型的值(对象)是引用类型的一个实例.在ECMAScript中,引用类型是一种数据结构,用于将数据和功能组织在一起.它也常被称为类,但这种称呼并不妥当(虽然不妥当,在别人提起的时候,就是指ECMAScript中的引用类型,在一次面试的时候,有人就和面试官争论说"JS中没有'类'这个概念,结果可想而知--").尽管ECMAScript从技术上讲是一门面向对象的语言,但它不举杯传统的面向对象语言所支持的类和接口等基本结构.引用类型有时候也被称为对象定义,因为它们描述的是一

关于变量 Objects...objects 和Object[] objects的区别

上一篇用到Objects...objects 和Object[] objects的遇到点小问题,于是我去做了个实验,关于这两个变量传参的问题 代码如下 package com.yck.test; public class ObjectsTest { public static String function(Object...objects) { return "success"; } public static String func(Object[] objs) { return

net.sf.json.JSONException: 'object' is an array. Use JSONArray instead

list集合转换JSON出错误 意思是:对象"是一个数组.使用jsonarray代替. 解决方法: 将JSONObject替换为JSONArray 代码: JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT); JSONArray json = JSONArray.fromObject(newList, jsonConfig);

'object' is an array. Use JSONArray instead

错误是:对象是一个数组,使用JSONArray代替 错误代码为:JSONObject json=JSONObject.fromObject(usertypeList); 正确代码为:JSONArray json=JSONArray.fromObject(usertypeList); 注意:JSONObject只是单个java对象转化json对象 'object' is an array. Use JSONArray instead

ExtJS学习-----------Ext.Array,ExtJS对javascript中的Array的扩展

关于ExtJS对javascript中的Array的扩展.能够參考其帮助文档,文档下载地址:http://download.csdn.net/detail/z1137730824/7748893 因为Array中的方法过多.将当中的部分方法设计实例进行学习.实例地址:http://blog.csdn.net/z1137730824/article/details/38797257 (1)Ext.Array中的方法 clean( Array array ) : Array 过滤掉数组里的空值,空值

Array.new(5, [1, 2, 3]) or Array.new(5) { [1, 2, 3] }的差别

Array.new(5, [1, 2, 3]) or Array.new(5) { [1, 2, 3] } Array.new(size, default_object) creates an array with an initial size, filled with the default object you specify. Keep in mind that if you mutate any of the nested arrays, you'll mutate all of th

[Lintcode]62. Search in Rotated Sorted Array/[Leetcode]33. Search in Rotated Sorted Array

[Lintcode]62. Search in Rotated Sorted Array/[Leetcode]33. Search in Rotated Sorted Array 本题难度: Medium/Medium Topic: Binary Search Description Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you beforehand. (