为什么JavaScript中的[] == []为false? [英] Why [] == [] is false in JavaScript?
问题描述
我正在处理代码的一部分,我有一个类似 [[data]]
的数组。 数据
通过Django模板引擎在服务器端呈现。所以我的代码如下所示:
I am working on a part of the code where I have an array which looks like [[data]]
. The data
is rendered on the server side through the Django template engine. So my code looks like this:
var data = {{ series|safe }};
// data will be [[]] if no data is present
if (data ==[[]])
console.log('no data');
如果
总是返回假
。这意味着在 [[]] == [[]]
是 false
,我的测试显示 [] == []
也是 false
。
The if
always returns false
. That means in [[]] == [[]]
is false
and my test shows that []==[]
is false
as well.
任何描述我将不胜感激。
Any descriptions would be appreciated.
推荐答案
因为 ==
(和 ===
)测试两个对象是否是相同的对象而不是相同的对象。
Because ==
(and ===
) test to see if two objects are the same object and not if they are identical objects.
大多数测试框架将包括 deepEqual $ c $等功能c>
如果你想看两个对象是否相同。
Most test frameworks will include functions such as deepEqual
if you want to see if two objects are identical.
这篇关于为什么JavaScript中的[] == []为false?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!