我如何检查这个 javascript 对象?

发布于 2024-11-06 08:04:53 字数 400 浏览 0 评论 0原文

{
    "@": {
        "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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半透明的墙 2024-11-13 08:04:53

假设您在 obj 中引用了它,那么:

if (obj && obj.SendEmailResult && "MessageId" in obj.SendEmailResult) {
    // The "MessageId" property exists in `obj.SendEmailResult`
}

不过,可能更有用:

var msgid = obj && obj.SendEmailResult && obj.SendEmailResult.MessageId;
if (msgid) {
    // The property exists and is "truthy", `msgid` is the value
}

JavaScript 的 AND 运算符 && 比其他一些语言的 AND 运算符更有用,它如果两个操作数都是“true”,则返回右侧的(而不是仅返回 true / false 结果,如大多数语言)。 “真”值是一个不“假”(显然)的值。 “Falsy”值为 falseundefinednull""0 >。

所以上面基本上是说“将 msgid 设置为 obj.SendEmailResult.MessageId 前提是 objobj.SomeEmailResult

|| 运算符是 同样强大。)

Assuming you have a reference to it in obj, then:

if (obj && obj.SendEmailResult && "MessageId" in obj.SendEmailResult) {
    // The "MessageId" property exists in `obj.SendEmailResult`
}

Probably more usefully, though:

var msgid = obj && obj.SendEmailResult && obj.SendEmailResult.MessageId;
if (msgid) {
    // The property exists and is "truthy", `msgid` is the value
}

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 a true / false result, as in most languages). A "truthy" value is a value that is not "falsy" (obviously). "Falsy" values are false, undefined, null, "", and 0.

So the above basically says "Set msgid to obj.SendEmailResult.MessageId provided that obj and obj.SomeEmailResult both exist.

(The || operator is similarly powerful.)

倚栏听风 2024-11-13 08:04:53

假设您在名为 result 的变量中获得结果,您应该能够使用简单的 if 语句来完成此操作(考虑到 javascript if 语句短路:

if (result && result["SendEmailResult"] && result["SendEmailResult"]["MessageId"]) {
    //execute your code
}

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:

if (result && result["SendEmailResult"] && result["SendEmailResult"]["MessageId"]) {
    //execute your code
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文