你如何检查一个变量是否是 JavaScript 中的数组? [英] How do you check if a variable is an array in JavaScript?

查看:22
本文介绍了你如何检查一个变量是否是 JavaScript 中的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 JavaScript 中检查变量是数组还是单个值.

I would like to check whether a variable is either an array or a single value in JavaScript.

我找到了一个可能的解决方案...

I have found a possible solution...

if (variable.constructor == Array)...

这是最好的方法吗?

推荐答案

有几种方法可以检查变量是否为数组.最好的解决方案是您选择的那个.

There are several ways of checking if an variable is an array or not. The best solution is the one you have chosen.

variable.constructor === Array

这是 Chrome 上最快的方法,很可能也是所有其他浏览器上的方法.所有数组都是对象,因此检查构造函数属性对于 JavaScript 引擎来说是一个快速的过程.

This is the fastest method on Chrome, and most likely all other browsers. All arrays are objects, so checking the constructor property is a fast process for JavaScript engines.

如果您在确定对象属性是否为数组时遇到问题,您必须首先检查该属性是否存在.

If you are having issues with finding out if an objects property is an array, you must first check if the property is there.

variable.prop && variable.prop.constructor === Array

其他一些方法是:

Array.isArray(variable)

使用 Chrome 75 于 2019 年 5 月 23 日更新,向@AnduAndrici 大声疾呼,让我用他的问题重新审视这个问题在我看来,最后一个是最丑陋的,也是 最慢 最快的之一.以第一个示例的速度运行大约 1/5. 这家伙慢了大约 2-5%,但很难说.用起来很扎实!对结果印象深刻.Array.prototype,其实就是一个数组.您可以在此处阅读更多相关信息 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray

Update May 23, 2019 using Chrome 75, shout out to @AnduAndrici for having me revisit this with his question This last one is, in my opinion the ugliest, and it is one of the slowest fastest. Running about 1/5 the speed as the first example. This guy is about 2-5% slower, but it's pretty hard to tell. Solid to use! Quite impressed by the outcome. Array.prototype, is actually an array. you can read more about it here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray

variable instanceof Array

作为第一个示例,此方法运行速度约为 1/3.如果您只关注漂亮的代码而不是性能,那么仍然非常可靠,看起来更干净.请注意,检查数字不起作用,因为 variable instanceof Number 总是返回 false.更新:instanceof 现在速度提高了 2/3!

This method runs about 1/3 the speed as the first example. Still pretty solid, looks cleaner, if you're all about pretty code and not so much on performance. Note that checking for numbers does not work as variable instanceof Number always returns false. Update: instanceof now goes 2/3 the speed!

又是一次更新

Object.prototype.toString.call(variable) === '[object Array]';

这个人在尝试检查数组时是最慢的.然而,这是您正在寻找的任何类型的一站式商店.但是,由于您正在寻找数组,因此只需使用上述最快的方法即可.

This guy is the slowest for trying to check for an Array. However, this is a one stop shop for any type you're looking for. However, since you're looking for an array, just use the fastest method above.

另外,我进行了一些测试:http://jsperf.com/instanceof-array-vs-array-isarray/35 所以找点乐子看看吧.

Also, I ran some test: http://jsperf.com/instanceof-array-vs-array-isarray/35 So have some fun and check it out.

注意:@EscapeNetscape 创建了另一个测试,因为 jsperf.com 已关闭.http://jsben.ch/#/QgYAV 我想确保在 jsperf 时保留原始链接重新上线.

Note: @EscapeNetscape has created another test as jsperf.com is down. http://jsben.ch/#/QgYAV I wanted to make sure the original link stay for whenever jsperf comes back online.

这篇关于你如何检查一个变量是否是 JavaScript 中的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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