6

是否有一个标准定义了如何比较 JavaScript,在 Chrome 控制台上我得到了这个

[10,0,0] > [1,0,0]
true
[10,0,0] > [5,0,0]
false
[5, 0, 0, 0] < [10, 0, 0, 0] //repeatable
false

[10,0,0,0] > [9,0,0,0]
false
[11,0,0,0] > [10,0,0,0]
true

这是非常不直观的,我什至无法理解正在应用什么逻辑,而且它们看起来是可重复的,所以看起来不是基于对象 id(ref) 等,所以有任何文档吗?

4

2 回答 2

4

JavaScript arrays are converted to strings and the strings are then compared. So.

[10,0,0].toString() => "10,0,0"
[5,0,0].toString() => "5,0,0"

Strings are compared lexicographically, so "5,0,0" is bigger than "10,0,0".

于 2013-06-06T06:56:18.403 回答
1

这样的事情可能会帮助你,

JSON.stringify([2,2,2]) === JSON.stringify([2,2,2]); //true

干杯:)。

于 2013-06-06T06:51:35.870 回答