为什么JavaScript中的[] == []为false? [英] Why [] == [] is false in JavaScript?

查看:113
本文介绍了为什么JavaScript中的[] == []为false?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理代码的一部分,我有一个类似 [[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 如果你想看两个对象是否相同。

Most test frameworks will include functions such as deepEqual if you want to see if two objects are identical.

这篇关于为什么JavaScript中的[] == []为false?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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