javascript Javascript检查字符串是否为真或假并转换为布尔值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19907479/
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
Javascript check if string if true or false and convert to Boolean
提问by ngplayground
Is there a better way to improve the below statement to check if the val()
is 'true'
or 'false'
and if it is then it will change it to a Boolean. The reason being, some of the values may be a word.
有没有更好的方法来改进下面的语句来检查是否val()
是'true'
或'false'
如果是则将其更改为布尔值。原因是,某些值可能是一个词。
var thisval = $(this).val();
if (thisval == "true"){
ao[id] = true;
} else if (thisval == "false"){
ao[id] = false;
} else {
ao[id] = $(this).val();
}
回答by Tibos
Most readable:
最易读:
var thisval = $(this).val();
ao[id] = thisval === 'true' ? true :
thisval === 'false' ? false :
thisval;
One-liner based on the conditional operator:
基于条件运算符的单行:
var thisval = $(this).val();
ao[id] = thisval === 'true' ? true : (thisval === 'false' ? false : thisval);
One-liner based on || and && behavior:
基于 || 的单线 和 && 行为:
var thisval = $(this).val();
ao[id] = thisval === 'true' || (thisval !== 'false') && thisval || false;
Shortest one-liner (combination of the above):
最短的单线(以上组合):
var thisval = $(this).val();
ao[id] = thisval === 'true' || (thisval === 'false' ? false : thisval);
回答by Khanh TO
Try JSON.parse().
试试JSON.parse()。
"true"
and "false"
are actually json representationsof true
, false
. This is how ajax parses json object as a string from server side. If on server side, we return true, false => the browser will receive it as a string "true" or "false" (json representation)
"true"
而"false"
实际上是JSON表示的true
,false
。这就是 ajax 如何从服务器端将 json 对象解析为字符串。如果在服务器端,我们返回 true,false => 浏览器将其作为字符串“true”或“false”接收(json 表示)
if ( $(this).val() == "true" || $(this).val() == "false") {
ao[id] = JSON.parse($(this).val());
}else {
ao[id] = $(this).val();
}
回答by Ankit Tyagi
String.prototype.bool = function() {
return (/^true$/i).test(this);
};
if ( $(this).val() == "true" || $(this).val() == "false") {
ao[id] = $(this).val().bool();
}else {
ao[id] = $(this).val();
}
回答by Strille
This might be slightlymore elegant:
这可能稍微优雅一些:
var thisval = $(this).val();
if (thisval === "true" || thisval === "false") {
thisval = (thisval === "true");
}
回答by vladzam
var thisval = $(this).val();
switch(thisval) {
case 'true' :
ao[id] = true;
break;
case 'false' :
ao[id] = false;
break;
default:
ao[id] = thisval;
}
I would create an array to hold the two possible values for when the string is true or false. Then, I would search to see if thisval
is found inside that array. If it is not, the indexOf
function will return -1. Afterwards, it is just a matter of going over those values and build up the cases that you want.
我会创建一个数组来保存字符串为真或假时的两个可能值。然后,我会搜索以查看是否thisval
在该数组中找到。如果不是,该indexOf
函数将返回 -1。之后,只需检查这些值并构建您想要的案例即可。
EDIT: I have updated the code based on the suggestion of Tibos. Thank you!
编辑:我已经根据 Tibos 的建议更新了代码。谢谢!
回答by Ratan Uday Kumar
In nodejs by using node-boolifywe can use isBoolean() & Boolify()
在 nodejs 中使用node-boolify我们可以使用 isBoolean() & Boolify()
Validation Results
验证结果
var isBoolean = require('node-boolify').isBoolean;
isBoolean(true); //true
isBoolean('true'); //true
isBoolean('TRUE'); //false
isBoolean(1); //true
isBoolean(2); //false
isBoolean(false); //true
isBoolean('false'); //true
isBoolean('FALSE'); //false
isBoolean(0); //true
isBoolean(null); //false
isBoolean(undefined); //false
isBoolean(); //false
isBoolean(''); //false
Boolean Conversion Results
布尔转换结果
var Boolify = require('node-boolify').Boolify;
Boolify(true); //true
Boolify('true'); //true
Boolify('TRUE'); //null
Boolify(1); //true
Boolify(2); //null
Boolify(false); //false
Boolify('false'); //false
Boolify('FALSE'); //null
Boolify(0); //false
Boolify(null); //null
Boolify(undefined); //null
Boolify(); //null
Boolify(''); //null