在UI中,我创建了一个对象,并将其中一个属性设置为布尔值:
function UserObject(componentId, componentName, checkedOut) {
this.componentId = componentId;
this.componentName = componentName;
this.checkedOut = checkedOut; //this is boolean variable
}
但在后台,当我在对象中设置布尔值时,json会将其转换为字符串。
private UserObject createUserObject(EntityDTO entity) {
UserObject userObject = new UserObject();
userObject.setComponentId(entity.getEntityId());
userObject.setComponentName(entity.getEntityName());
userObject.setCheckedOut(entity.getCheckedOut());
return userObject;
}
现在,问题是,我两次匹配一些条件(1(,同时创建(2(,然后从后端获取数据。每当我匹配"checkedOut"对象的条件时,当对象来自后端时,它就会失败:
if(cell.value.checkedOut === true){
//some code
}else{
//some more code
}
我该怎么办?提前感谢:(
if(cell.value.checkedOut === "true"){
//some code
}else{
//some more code
}
由于它是json中的一个字符串,现在使用双引号进行比较
如果要将字符串"true或false"转换为布尔类型,请使用eval()
:
if(eval(cell.value.checkedOut) === true) {
//some code
} else {
//some more code
}