Jquery 如何检查JSON中是否存在属性及其值是否为true
我有一个JSON,如下所示Jquery 如何检查JSON中是否存在属性及其值是否为true,jquery,Jquery,我有一个JSON,如下所示 { "name": "mark", "age": 35, "isActive": true } 我需要检查属性isActive是否存在,它是否也是真的 我试过下面的工作,但想问有没有比下面更好的方法 var test = { "name": "mark", "age": 35, "isActive": true } if(test.isActive && test.isActive==true) {
{
"name": "mark",
"age": 35,
"isActive": true
}
我需要检查属性isActive是否存在,它是否也是真的
我试过下面的工作,但想问有没有比下面更好的方法
var test = {
"name": "mark",
"age": 35,
"isActive": true
}
if(test.isActive && test.isActive==true)
{
alert('yes')
}
只要使用
if (test.isActive === true)
如果该属性不存在,this.isActive
的值将是未定义的
,这不等于true
使用==
进行严格的类型检查,这样它就不会强制其他类型并给出错误的结果
如果属性存在时始终包含布尔值,则可以使用:
if (test.isActive)
只用
if (test.isActive === true)
如果该属性不存在,this.isActive
的值将是未定义的
,这不等于true
使用==
进行严格的类型检查,这样它就不会强制其他类型并给出错误的结果
如果属性存在时始终包含布尔值,则可以使用:
if (test.isActive)
您可以使用名为“hasOwnProperty()”的json内置函数来检查这一点。请尝试下面的方式,它工作正常
var test = {
"name": "mark",
"age": 35,
"isActive": true
}
if(test.hasOwnProperty("isActive") && test.isActive===true){
alert('yes');
}else{
alert("no");
}
您可以使用名为“hasOwnProperty()”的json内置函数来检查这一点。请尝试下面的方式,它工作正常
var test = {
"name": "mark",
"age": 35,
"isActive": true
}
if(test.hasOwnProperty("isActive") && test.isActive===true){
alert('yes');
}else{
alert("no");
}
test.isActive
已足够定义为“更好”。如果(test.isActive&&test.isActive==true)不够好,那么呢?这是一行代码。这不是一个足够快的执行时间吗?test.isActive
足够好了吗。如果(test.isActive&&test.isActive==true)
不够好,那么呢?这是一行代码。执行时间不够快吗?hasOwnProperty
正是作者想要的hasOwnProperty
与测试数据传输格式json完全无关。hasOwnProperty是一种对象协议方法hasOwnProperty
正是作者想要的hasOwnProperty
与测试数据传输格式json完全无关。hasOwnProperty是一种对象协议方法