我如何检查这个 javascript 对象?
{
"@": {
"xmlns": "http://ses.amazonaws.com/doc/2010-12-01/"
},
"SendEmailResult": {
"MessageId": "0000012fdd10caaf-021c6e9e-e872-4b35-ad94-1d11c79a6324-000000"
},
"ResponseMetadata": {
"RequestId": "736d5bb2-7b7d-11e0-b435-f7b0c9315f0d"
}
}
如何检查对象中是否存在“MessageId”? (不抛出错误) 我可能会返回其他 json 对象,并且我需要知道我得到的对象是否有“MessageId”。
{
"@": {
"xmlns": "http://ses.amazonaws.com/doc/2010-12-01/"
},
"SendEmailResult": {
"MessageId": "0000012fdd10caaf-021c6e9e-e872-4b35-ad94-1d11c79a6324-000000"
},
"ResponseMetadata": {
"RequestId": "736d5bb2-7b7d-11e0-b435-f7b0c9315f0d"
}
}
How do I check if "MessageId" exists in a object? (without throwing errors)
I might get other json objects returned, and I need to know if the one I get has a "MessageId".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您在
obj
中引用了它,那么:不过,可能更有用:
JavaScript 的 AND 运算符
&&
比其他一些语言的 AND 运算符更有用,它如果两个操作数都是“true”,则返回右侧的值(而不是仅返回true
/false
结果,如大多数语言)。 “真”值是一个不“假”(显然)的值。 “Falsy”值为false
、undefined
、null
、""
和0
>。所以上面基本上是说“将
msgid
设置为obj.SendEmailResult.MessageId
前提是obj
和obj.SomeEmailResult
都(
||
运算符是 同样强大。)Assuming you have a reference to it in
obj
, then:Probably more usefully, though:
JavaScript's AND operator
&&
is more useful than that of some other languages, it returns the right-hand side's value if both of its operands are "truthy" (rather than just returning atrue
/false
result, as in most languages). A "truthy" value is a value that is not "falsy" (obviously). "Falsy" values arefalse
,undefined
,null
,""
, and0
.So the above basically says "Set
msgid
toobj.SendEmailResult.MessageId
provided thatobj
andobj.SomeEmailResult
both exist.(The
||
operator is similarly powerful.)假设您在名为 result 的变量中获得结果,您应该能够使用简单的 if 语句来完成此操作(考虑到 javascript if 语句短路:
Assuming you get the result in a variable called result, you should be able to do this with a simple if statement (considering that the javascript if statement short circuits: