Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
313 views
in Technique[技术] by (71.8m points)

javascript - 如何检查变量是否是JavaScript中的数组? [重复](How do you check if a variable is an array in JavaScript? [duplicate])

This question already has an answer here:

(这个问题在这里已有答案:)

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

(我想检查变量是JavaScript中的数组还是单个值。)

I have found a possible solution...

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

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

Is this the best way this can be done?

(这是最好的办法吗?)

  ask by Andy McCluggage translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

This is the fastest method on Chrome, and most likely all other browsers.

(这是Chrome上最快的方法,很可能是所有其他浏览器。)

All arrays are objects, so checking the constructor property is a fast process for JavaScript engines.

(所有数组都是对象,因此检查构造函数属性是JavaScript引擎的快速过程。)

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

Some other ways are:

(其他一些方法是:)

Array.isArray(variable)

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.

(使用Chrome 75更新于2019年5月23日,向@AnduAndrici致敬,让我用他的问题重新审视这个问题 。在我看来,最后一个是最丑的,它是最慢的一个。)

Running about 1/5 the speed as the first example.

(运行速度约为第一个例子的1/5。)

This guy is about 2-5% slower, but it's pretty hard to tell.

(这家伙慢了约2-5%,但很难说。)

Solid to use!

(坚固使用!)

Quite impressed by the outcome.

(对结果印象深刻。)

Array.prototype, is actually an array.

(Array.prototype实际上是一个数组。)

you can read more about it here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray

(你可以在这里阅读更多相关信息https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray)

variable instanceof Array

This method runs about 1/3 the speed as the first example.

(该方法的速度约为第一个例子的1/3 。)

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 .

(请注意,检查数字不起作用variable instanceof Number始终返回false 。)

Update: instanceof now goes 2/3 the speed!

(更新: instanceof现在速度提高了2/3!)

So yet another update

(还有另一个更新)

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.

(但是,由于您正在寻找一个数组,只需使用上面最快的方法。)

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

(此外,我进行了一些测试: http//jsperf.com/instanceof-array-vs-array-isarray/35所以有一些乐趣并检查出来。)

Note: @EscapeNetscape has created another test as jsperf.com is down.

(注意:@EscapeNetscape在jsperf.com关闭时创建了另一个测试。)

http://jsben.ch/#/QgYAV I wanted to make sure the original link stay for whenever jsperf comes back online.

(http://jsben.ch/#/QgYAV我想确保只要jsperf重新上线,原始链接就会保留。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...