jQuery 如何在模型获取时将 1 转换为 true 或将 0 转换为 false
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16313222/
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 to convert 1 to true or 0 to false upon model fetch
提问by Chris M
I have a model that is set with a JSON response from a mysql database. The model data is set with true or false into a boolean/tinyint field in the database, which uses 1
or 0
.
我有一个使用来自 mysql 数据库的 JSON 响应设置的模型。模型数据使用 true 或 false 设置到数据库中的 boolean/tinyint 字段中,该字段使用1
or 0
。
In my view, I have a binding that checks for a boolean with underscore's _.isBoolean
. Of course, when my model receives the data, it is set with 1
or 0
instead of true or false and the _.isBoolean
check fails.
在我看来,我有一个绑定来检查带有下划线的_.isBoolean
. 当然,当我的模型接收到数据时,它被设置为1
或0
代替真或假,_.isBoolean
检查失败。
Is there anyway to either correctly have my JSON response from mysql be a boolean true or false value instead of 1
or 0
, or preferably, is there a way to have my model update itself upon fetch (and before the view renders) to cast true
or false
based on it's 1 or 0 property?
无论如何,是否可以正确地让我的来自 mysql 的 JSON 响应是布尔值 true 或 false 值而不是1
or 0
,或者最好是有办法让我的模型在获取时(以及在视图呈现之前)自我更新以进行转换true
或false
基于它是 1 还是 0 属性?
e.g. my model's data looks like {"isChecked":"1"}
when I need it to be {"isChecked":true}
例如,我的模型数据{"isChecked":"1"}
在我需要时看起来像{"isChecked":true}
Thank you greatly for any suggestions you may have!
非常感谢您的任何建议!
回答by Vitalii Petrychuk
All you need is convert string
to int
with +
and convert the result to boolean with !!
:
您只需要转换string
为int
with+
并将结果转换为布尔值!!
:
var response = {"isChecked":"1"};
response.isChecked = !!+response.isChecked
You can do this manipulation in the parse
method:
您可以在parse
方法中进行此操作:
parse: function (response) {
response.isChecked = !!+response.isChecked;
return response;
}
UPDATE: 7 years later, I find Number(string)
conversion more elegant. Also mutating an object is not the best idea. That being said:
更新:7 年后,我发现Number(string)
转换更优雅。也改变一个对象不是最好的主意。话虽如此:
parse: function (response) {
return Object.assign({}, response, {
isChecked: !!Number(response.isChecked), // OR
isChecked: Boolean(Number(response.isChecked))
});
}
回答by dave
Use a double not:
使用双不:
!!1 = true;
!!0 = false;
obj.isChecked = !!parseInt(obj.isChecked);
回答by bmaupin
Here's another option that's longer but may be more readable:
这是另一个更长但可能更具可读性的选项:
Boolean(Number("0")); // false
Boolean(Number("1")); // true
回答by SomeShinyObject
Assigning Comparison to property value
将比较分配给属性值
JavaScript
JavaScript
You could assign the comparison of the property to "1"
您可以将属性的比较分配给 "1"
obj["isChecked"] = (obj["isChecked"]==="1");
This only evaluates for a String value of "1"
though. Other variables evaulate to false like an actual typeof number
would be false. (i.e. obj["isChecked"]=1
)
这仅评估字符串值"1"
。其他变量评估为假,就像实际为假一样typeof number
。(即obj["isChecked"]=1
)
If you wanted to be indiscrimate about "1"
or 1
, you could use:
如果你想不分青红皂白地使用"1"
or 1
,你可以使用:
obj["isChecked"] = (obj["isChecked"]=="1");
Example Outputs
示例输出
console.log(obj["isChecked"]==="1"); // true
console.log(obj["isChecked"]===1); // false
console.log(obj["isChecked"]==1); // true
console.log(obj["isChecked"]==="0"); // false
console.log(obj["isChecked"]==="Elephant"); // false
PHP
PHP
Same concept in PHP
PHP中的相同概念
$obj["isChecked"] = ($obj["isChecked"] == "1");
The same operator limitations as stated above for JavaScript apply.
与上述 JavaScript 相同的运算符限制适用。
Double Not
双不
The 'double not' also works. It's confusing when people first read it but it works in both languages for integer/number type values. It however does not work in JavaScript for string type values as they always evaluate to true:
“双重不”也有效。人们第一次阅读它时会感到困惑,但它适用于整数/数字类型值的两种语言。然而,它在 JavaScript 中不适用于字符串类型值,因为它们总是评估为真:
JavaScript
JavaScript
!!"1"; //true
!!"0"; //true
!!1; //true
!!0; //false
!!parseInt("0",10); // false
PHP
PHP
echo !!"1"; //true
echo !!"0"; //false
echo !!1; //true
echo !!0; //false