Javascript!==返回true时应该返回false [重复]

时间:2023-02-06 02:32:57

This question already has an answer here:

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

I am using a Node.js server and async.js to handle asynchronous callbacks, as well as Mongoose to connect to my Mongo datastore. I am attempting to determine if two object _id's are equal, and if so, execute some code. However, the comparison is not executing correctly. Here's the code:

我使用Node.js服务器和async.js来处理异步回调,以及使用Mongoose连接到我的Mongo数据存储区。我试图确定两个对象_id是否相等,如果是,则执行一些代码。但是,比较没有正确执行。这是代码:

async.forEachSeries(offerItem.offers, function(currentOfferItemOffer, callback) {
    Offer.findById(currentOfferItemOffer, function(err, offerItemOffer) {
        if (err) throw err;
        console.log(offerItemOffer._id) // 56953639ea526c8352081fdd
        console.log(offer._id)          // 56953639ea526c8352081fdd
        if (offerItemOffer._id !== offer._id) {
            console.log('offerItemOffer._id !== offer._id') // Is being logged even though it shouldn't

            ....

I am very confused as to why a simple comparison like this would not be executing correctly. The code functions as desired when the two _id's are checked for equality using '===' - but this is incorrect logically, as the next block should only be executed when the _id's are not equal. Any thoughts would be greatly appreciated. Thanks!

我很困惑为什么像这样的简单比较不能正确执行。当使用'==='检查两个_id的相等性时,代码按照需要运行 - 但这在逻辑上是不正确的,因为下一个块只应在_id不相等时执行。任何想法将不胜感激。谢谢!

3 个解决方案

#1


0  

Use toString() method on _id.

在_id上使用toString()方法。

if (offerItemOffer._id.toString() !== offer._id.toString()) {//...

console.log() calls toString() so it seems that output is the same because it is converted to string.

console.log()调用toString()所以看起来输出是相同的,因为它被转换为字符串。

#2


0  

It looks like _id are objects, not strings. In this case they are still two different object. You should do:

它看起来像_id是对象,而不是字符串。在这种情况下,它们仍然是两个不同的对象。你应该做:

JSON.stringify(offerItemOffer._id) !== JSON.stringify(offer.-id) 

to compare them as strings.

将它们比作字符串。

More here Object comparison in JavaScript

更多这里JavaScript中的对象比较

#3


0  

JavaScript has two types of inequality operators. Namely != and !==.

JavaScript有两种类型的不等式运算符。即!=和!==。

The != calls stringify upon operators before comparison. That means that the type/class of a given operator isn't taken into account when comparing.

在比较之前,!=调用运算符stringify。这意味着在比较时不考虑给定运算符的类型/类。

The !== doesn't call stringfy. That means that the type/class of an operator is taken into account.

!==不会调用stringfy。这意味着要考虑运营商的类型/类别。

That's why the following sentences produce distinct outputs

这就是为什么以下句子产生不同的输出

'1' != 1   // -> false (they are equal since what's being compared is '1' != '1'
'1' !== 1  // -> true  (they are different since what's being compared is a string with an integer)

Thus you could fix your problem by ignoring object types/classes by using the != operator.

因此,您可以通过使用!=运算符忽略对象类型/类来解决您的问题。

#1


0  

Use toString() method on _id.

在_id上使用toString()方法。

if (offerItemOffer._id.toString() !== offer._id.toString()) {//...

console.log() calls toString() so it seems that output is the same because it is converted to string.

console.log()调用toString()所以看起来输出是相同的,因为它被转换为字符串。

#2


0  

It looks like _id are objects, not strings. In this case they are still two different object. You should do:

它看起来像_id是对象,而不是字符串。在这种情况下,它们仍然是两个不同的对象。你应该做:

JSON.stringify(offerItemOffer._id) !== JSON.stringify(offer.-id) 

to compare them as strings.

将它们比作字符串。

More here Object comparison in JavaScript

更多这里JavaScript中的对象比较

#3


0  

JavaScript has two types of inequality operators. Namely != and !==.

JavaScript有两种类型的不等式运算符。即!=和!==。

The != calls stringify upon operators before comparison. That means that the type/class of a given operator isn't taken into account when comparing.

在比较之前,!=调用运算符stringify。这意味着在比较时不考虑给定运算符的类型/类。

The !== doesn't call stringfy. That means that the type/class of an operator is taken into account.

!==不会调用stringfy。这意味着要考虑运营商的类型/类别。

That's why the following sentences produce distinct outputs

这就是为什么以下句子产生不同的输出

'1' != 1   // -> false (they are equal since what's being compared is '1' != '1'
'1' !== 1  // -> true  (they are different since what's being compared is a string with an integer)

Thus you could fix your problem by ignoring object types/classes by using the != operator.

因此,您可以通过使用!=运算符忽略对象类型/类来解决您的问题。