【发布时间】:2016-04-05 04:29:02
【问题描述】:
我有一个对象:
messages = {
V1: {
summary: "summary one",
cause: "cause one",
code: "1"
},
V2: {
summary: "summary two",
cause: "cause two,
code: "2"
}
我想将我的 event.details 的值与对象消息的键进行比较,并为匹配键的摘要、原因和代码设置变量。
到目前为止我的实现:
if (event.details === Object.keys(messages)) {
var a = the summary of the matching key;
var b = the cause of the matching key;
var c = the code for the matching key;
};
后来我在我的代码中使用了这些变量.... 目前我的结果是:
event.details = "V1"
Object.Keys(messages) = ["V1","V2"]
但这只是给了我一个键数组。我现在想获取匹配键的信息。
如何检查密钥是否与 event.details 匹配?以及如何将变量设置为key的summary、cause和code?p>
【问题讨论】:
-
因为最后少了一个 "
-
Object.keys 返回一个数组,它是一个对象,所以它永远只会是
===给它自己。
标签: javascript object for-loop