[英]Javascript dictionary key exists within conditional statement
I'm encountering some strange behavior while working with a dictionary object in Node.JS as part of a MEAN stack project. 作为MEAN堆栈项目的一部分,在Node.JS中使用字典对象时遇到一些奇怪的行为。
I define a keywordSearches
dictionary earlier in my code, and searches
is an array of Search
objects that contains a keyword
property. 我定义了一个keywordSearches
在我前面的代码字典, searches
是数组Search
的对象包含keyword
属性。 I'm essentially pulling from MongoDB a record of all my search requests, and then creating a dictionary containing the frequency of the keyword searches, with the key being the search text, and the value being the frequency of it being searched (an integer). 我实质上是从MongoDB中提取所有搜索请求的记录,然后创建一个包含关键字搜索频率的字典,其中键为搜索文本,值为被搜索频率(整数) 。 All this is stored in keywordSearches
. 所有这些都存储在keywordSearches
。
However, when I use the following code to iterate through my searches, I see the keywords in keywordSearches
evaluates to false outside of my if condition, but then apparently evaluates to true within the if condition (the very next line!). 但是,当我使用以下代码遍历搜索时,我看到在if条件之外, keywordSearches
的关键字评估为false,但显然在if条件中(下一行!)评估为true。 Why is this occurring? 为什么会这样呢?
console.log(keywordSearches);
for (var i = 0; i < searches.length; i++){
var keywords = searches[i].searchBody.keywords;
console.log(keywords in keywordSearches); // <- this evaluates to false
if (!keywords in keywordSearches){ // <- this section of code never executes! Why?
console.log("New keyword found")
keywordSearches[keywords] = 1;
} else {
keywordSearches[keywords] = keywordSearches[keywords] + 1;
console.log("else statement")
}
}
console.log(keywordSearches);
Output (note, I have four Search
objects, all with keyword "photography": 输出 (请注意,我有四个Search
对象,所有对象均带有关键字“ photography”:
{} <- console.log(keywordSearches)
false <- (keywords in keyWord Searches)
else statement <- if condition evaluates to false! Should evaluate to true. Why?
true
else statement
true
else statement
true
else statement
true
else statement
{ photography: NaN }
I understand why photography
is NaN
: it's never initialized with a value of 1
. 我知道photography
为什么是NaN
:它永远不会初始化为1
的值。 (which it should if it's initially not found in the dictionary). (如果最初在字典中未找到该字符,则应该使用该字符)。 So it's adding NaN
+ 1 each time. 因此,每次添加NaN
+1。
in
has lower precedence than !
in
优先级低于!
, so your expression is evaluated as: ,因此您的表达式的计算结果为:
(!keywords) in keywordSearches
Instead of: 代替:
!(keywords in keywordSearches)
See: Operator precedence on MDN 请参阅:MDN上的运算符优先级
Avoid using !
避免使用!
completely, and switch the if-else
statements instead: 完全,然后切换if-else
语句:
console.log(keywordSearches);
for (var i = 0; i < searches.length; i++){
var keywords = searches[i].searchBody.keywords;
console.log(keywords in keywordSearches); // <- this evaluates to false
if (keywords in keywordSearches){
keywordSearches[keywords] = keywordSearches[keywords] + 1;
console.log("keyword already exists")
} else {
console.log("New keyword found")
keywordSearches[keywords] = 1;
}
}
console.log(keywordSearches);
This saves on an operation. 这节省了操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.