为什么或如何证明 JavaScript 数组相等?
在这个答案中有一个简单的函数,它将返回包含原始值的数组的数组相等性。
但是,我不确定它为什么有效。这是功能:
function arrays_equal(a,b) { return !!a && !!b && !(a<b || b<a); }
我最感兴趣的是后半部分;这一点:
!(a<b || b<a)
为什么在比较数组时 <
和 >
可以工作,而 ==
却不行?
JavaScript 中的小于和大于方法如何工作?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
<
/>
时,数组首先被转换为字符串,因此不提供可靠的检查相等性的方法。==
不起作用,因为对象是通过引用检查的:<
/>
技巧有缺陷:其计算结果为
true
,因为它们在检查之前都被转换为字符串"1,2,3"
(<
/>
不要“直接”对对象起作用)。所以基本上,您正在比较字符串的相等性。对于字符串,
a == b
确实与!(a -
<
/>
对于字符串检查字符代码,因此两个相等的字符串既不会“更小”也不会“更大”,因为字符串中的任何字符代码都不是这种情况。With
<
/>
, the arrays are converted to strings first, and as such do not provide a reliable method of checking equality.==
does not work because objects are checked by reference:The
<
/>
trick is flawed:This evaluates to
true
, because they are both converted to the string"1,2,3"
before they are checked (<
/>
do not "directly" work for objects).So basically, you are comparing equality of the strings. For strings,
a == b
is indeed the same as!(a<b || b<a)
-<
/>
for strings check character codes, so two equal strings are neither "smaller" nor "greater" because that's not the case for any character code in the strings.这不起作用。 考虑也会
即使根据基于元素比较的数组相等的任何定义,
产生 true,它们是不同的。并且
也是假阳性。
简单地添加
length
检查不会有帮助,如编辑所示:
如果您想要一种简洁的方法来测试结构相似性,我建议:
它不会在明显不同的输入上提前停止 - 它会行走两个对象图,无论它们有多么不同,但OP中的函数也是如此。
需要注意的是:当您使用非本机
JSON.stringify
时,它可能会对循环输入执行奇怪的操作,例如:It doesn't work. Consider
produces true even though by any definition of array equality based on element-wise comparison, they are different.
and
are also spurious positives.
Simply adding
length
checking won't help as demonstrated byEDIT:
If you want a succinct way to test structural similarity, I suggest:
It doesn't stop early on inputs that are obviously different -- it walks both object graphs regardless of how disimilar they are, but so does the function in the OP.
One caveat: when you're using non-native
JSON.stringify
, it may do strange things for cyclic inputs like:您可以使用
==
比较任意两个对象。但自从>且<不是为对象定义的,它们被转换为字符串。因此,[1,2,3]>[2,1,3]
实际上是在做"1,2,3">"2,1,3"
You can compare any two objects using
==
. But since > and < are not defined for objects, they are converted to strings. Therefore,[1,2,3]>[2,1,3]
is actually doing"1,2,3">"2,1,3"