在javascript中我们如何识别对象是Hash还是数组? [英] In javascript how can we identify whether an object is a Hash or an Array?

查看:107
本文介绍了在javascript中我们如何识别对象是Hash还是数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的JSON调用的输出可以是数组或哈希。我如何区分这两者?

The output of my JSON call can either be an Array or a Hash. How do I distinguish between these two?

推荐答案

现代浏览器支持 Array.isArray( obj)方法。

Modern browsers support the Array.isArray(obj) method.

请参阅MDN 以获取文档和polyfill。

See MDN for documentation and a polyfill.

= 2008年的原始答案 =

您可以使用输出的constuctor属性:

you can use the constuctor property of your output:

if(output.constructor == Array){
}
else if(output.constructor == Object){
}

这篇关于在javascript中我们如何识别对象是Hash还是数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆