amt: "10.00"
email: "sam@gmail.com"
merchant_id: "sam"
mobileNo: "9874563210"
orderID: "123456"
passkey: "1234"

以上就是我要处理的JSON对象。我想检查merchant_id键是否存在。我尝试了下面的代码,但它不工作。有办法实现吗?

<script>
window.onload = function getApp()
{
  var thisSession = JSON.parse('<?php echo json_encode($_POST); ?>');
  //console.log(thisSession);
  if (!("merchant_id" in thisSession)==0)
  {
    // do nothing.
  }
  else 
  {
    alert("yeah");
  }
}
</script>

当前回答

(尽管我迟到了,但我还是想指出这一点) 你最初的问题本质上是想找到一个“Not IN”。 这似乎没有得到我正在做的研究(下面2个链接)的支持。

所以如果你想做一个Not In

("merchant_id" in x)
true
("merchant_id_NotInObject" in x)
false 

我建议 只需将表达式==设置为您要查找的内容

if (("merchant_id" in thisSession)==false)
{
    // do nothing.
}
else 
{
    alert("yeah");
}

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in http://www.w3schools.com/jsref/jsref_operators.asp

其他回答

你可以试试if(typeof object !== 'undefined')

(尽管我迟到了,但我还是想指出这一点) 你最初的问题本质上是想找到一个“Not IN”。 这似乎没有得到我正在做的研究(下面2个链接)的支持。

所以如果你想做一个Not In

("merchant_id" in x)
true
("merchant_id_NotInObject" in x)
false 

我建议 只需将表达式==设置为您要查找的内容

if (("merchant_id" in thisSession)==false)
{
    // do nothing.
}
else 
{
    alert("yeah");
}

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in http://www.w3schools.com/jsref/jsref_operators.asp

试试这个,

if(thisSession.hasOwnProperty('merchant_id')){

}

JS对象thisSession应该像这样

{
amt: "10.00",
email: "sam@gmail.com",
merchant_id: "sam",
mobileNo: "9874563210",
orderID: "123456",
passkey: "1234"
}

你可以在这里找到细节

我稍微改变你的if语句和工作(也为继承的obj -看片段)

if(!("merchant_id" in thisSession)) alert("yeah");

var sessionA = { amt: "10.00", email: "sam@gmail.com", merchant_id: "sam", mobileNo: "9874563210", orderID: "123456", passkey: "1234", } var sessionB = { amt: "10.00", email: "sam@gmail.com", mobileNo: "9874563210", orderID: "123456", passkey: "1234", } var sessionCfromA = Object.create(sessionA); // inheritance sessionCfromA.name = 'john'; if (!("merchant_id" in sessionA)) alert("merchant_id not in sessionA"); if (!("merchant_id" in sessionB)) alert("merchant_id not in sessionB"); if (!("merchant_id" in sessionCfromA)) alert("merchant_id not in sessionCfromA"); if ("merchant_id" in sessionA) alert("merchant_id in sessionA"); if ("merchant_id" in sessionB) alert("merchant_id in sessionB"); if ("merchant_id" in sessionCfromA) alert("merchant_id in sessionCfromA");

函数检查未定义和空对象

function elementCheck(objarray, callback) {
        var list_undefined = "";
        async.forEachOf(objarray, function (item, key, next_key) {
            console.log("item----->", item);
            console.log("key----->", key);
            if (item == undefined || item == '') {
                list_undefined = list_undefined + "" + key + "!!  ";
                next_key(null);
            } else {
                next_key(null);
            }
        }, function (next_key) {
            callback(list_undefined);
        })
    }

这是一个简单的方法来检查对象发送是否包含未定义或空

var objarray={
"passenger_id":"59b64a2ad328b62e41f9050d",
"started_ride":"1",
"bus_id":"59b8f920e6f7b87b855393ca",
"route_id":"59b1333c36a6c342e132f5d5",
"start_location":"",
"stop_location":""
}
elementCheck(objarray,function(list){
console.log("list");
)