如何在 Javascript 中比较字符串和布尔值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5659085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I compare string and boolean in Javascript?
提问by alex
I got the Json "false"
from server. I respond as bool
but it's Json so it's in browser type is String
instead of bool
.
我"false"
从服务器得到了 Json 。我回应为bool
但它是 Json 所以它在浏览器类型中是String
而不是bool
.
So if I run (!data)
whenever I want to check "false" == false
then they not worked.
因此,如果我(!data)
想检查任何时候都运行,"false" == false
那么它们就不起作用了。
So how can I parse bool
from String
in JavaScript then?
所以,我怎么能解析bool
从String
JavaScript中呢?
"true" == true
and "false" == false
. Then the code (!data)
can check what it is [true
and false
]
"true" == true
和"false" == false
。然后代码(!data)
可以检查它是什么 [true
和false
]
回答by alex
I would just explicitly check for the string "true"
.
我只会明确检查 string "true"
。
let data = value === "true";
Otherwise you could use JSON.parse()
to convert it to a native JavaScript value, but it's a lot of overhead if you know it's only the strings "true"
or "false"
you will receive.
否则,您可以使用JSON.parse()
将其转换为原生 JavaScript 值,但如果您知道它只是字符串,"true"
否则"false"
您将收到大量开销。
回答by Yukulélé
If one of the operands is Boolean, the Boolean operand is converted to 1 if it is true and +0 if it is false.(from MDN Comparison Operatorspage)
如果操作数之一是布尔值,则布尔操作数在为真时转换为 1,如果为假则转换为 +0。(从MDN比较运算符页)
Examples:
例子:
true == "true"; //false
true == "1"; //true
false == "false"; //false
false == ""; //true
false == "0"; //true
回答by Yura Galavay
var data = true;
data === "true" //false
String(data) === "true" //true
This works fine.
这工作正常。
回答by simplyharsh
If its just a json "false"/"true", you can use,
如果它只是一个 json "false"/"true",你可以使用,
if(! eval(data)){
// Case when false
}
It would be more cleaner, if you restrict the code to accept only JSON data from server, and always jsonParse or eval it to JS object (something like jquery getJSON does. It accepts only JSON responses and parse it to object before passing to callback function).
如果您将代码限制为仅接受来自服务器的 JSON 数据,并且始终将其 jsonParse 或 eval 评估为 JS 对象(例如 jquery getJSON 所做的事情。它只接受 JSON 响应并在传递给回调函数之前将其解析为对象),这会更清晰)。
That way you'll not only get boolean as boolean-from-server, but it will retain all other datatypes as well, and you can then go for routine expressions statements rather than special ones.
这样,您不仅可以将 boolean 作为 boolean-from-server 获得,而且还将保留所有其他数据类型,然后您可以使用常规表达式语句而不是特殊语句。
Happy Coding.
快乐编码。
回答by Larry K
Try expression data == "true"
尝试表达式数据 == "true"
Tests:
测试:
data = "false" -- value will be false
data = "false" -- 值为假
date = "true" -- value will be true
date = "true" -- 值为真
Also, fix your JSON. JSON can handle booleans just fine.
另外,修复您的 JSON。JSON 可以很好地处理布尔值。
回答by AndrewR
I think you need to look at how the JSON data is being generated. You can definitely have a normal JS boolean false in JSON.
我认为您需要查看 JSON 数据是如何生成的。你绝对可以在 JSON 中有一个普通的 JS boolean false。
{ "value1" : false, "value2" : true }
{“值1”:假,“值2”:真}
回答by jn_pdx
String.prototype.revalue= function(){
if(/^(true|false|null|undefined|NaN)$/i.test(this)) return eval(this);
if(parseFloat(this)+''== this) return parseFloat(this);
return this;
}
From: http://www.webdeveloper.com/forum/showthread.php?t=147389
来自:http: //www.webdeveloper.com/forum/showthread.php?t=147389
Actually, you just need the first "if" statement from the function -- tests to find true or false in the code and the eval
s it, turning it into the boolean value
实际上,您只需要函数中的第一个“if”语句 - 测试以在代码中查找 true 或 falseeval
并将其转换为布尔值
回答by gifpif
if(data+''=='true'){
alert('true');
}
Convert boolean to string by appending with blank string. and then compare with Stringobject.
通过附加空白字符串将布尔值转换为字符串。然后与 Stringobject 进行比较。
回答by kheengz
To compare string and Boolean in JavaScript, let us see the following examples. It returns true:
为了比较 JavaScript 中的字符串和布尔值,让我们看看下面的例子。它返回真:
false == "0"; //true
true == "1"; //true
false == ""; //true
The followings returns false:
以下返回错误:
false == "false"; //false
true == "true"; //false
In addition, try the following example too:
此外,也请尝试以下示例:
var data = true;
data === "true" //false
String(data) === "true" //true
https://www.tutorialspoint.com/How-do-I-compare-String-and-Boolean-in-JavaScript
https://www.tutorialspoint.com/How-do-I-compare-String-and-Boolean-in-JavaScript