检查字符串是否包含在特定的 Map() 键中
Check if a string is contained within a specific Map() key
我有一张从 json 文件填充的地图,类似于下面的
key: ConfigItem
value: Var1,Var2,Var3
key: ConfigItem2
value: Var1,Var2,Var3,var4
key: ConfigItem3
value: true
我希望能够 运行 一个 if 语句来检查一个值是否包含在 "ConfigItem" 键中,如果是,则做一些事情。
我查看了 map.get 和 map.has,但我似乎无法弄清楚如何搜索特定键和 return(如果它包含特定值)。
您似乎在寻找
const map = new Map([
["ConfigItem", ["Var1","Var2","Var3"]],
["ConfigItem2", ["Var1","Var2","Var3","var4"]],
["ConfigItem3", true],
]);
if (map.get("ConfigItem").includes("Var2")) {
…
}
注意这只是使用 Map get
to access the array value for the ConfigItem
key, but then uses the normal Array includes
method 来检查特定字符串是否包含在其中。
我有一张从 json 文件填充的地图,类似于下面的
key: ConfigItem
value: Var1,Var2,Var3
key: ConfigItem2
value: Var1,Var2,Var3,var4
key: ConfigItem3
value: true
我希望能够 运行 一个 if 语句来检查一个值是否包含在 "ConfigItem" 键中,如果是,则做一些事情。
我查看了 map.get 和 map.has,但我似乎无法弄清楚如何搜索特定键和 return(如果它包含特定值)。
您似乎在寻找
const map = new Map([
["ConfigItem", ["Var1","Var2","Var3"]],
["ConfigItem2", ["Var1","Var2","Var3","var4"]],
["ConfigItem3", true],
]);
if (map.get("ConfigItem").includes("Var2")) {
…
}
注意这只是使用 Map get
to access the array value for the ConfigItem
key, but then uses the normal Array includes
method 来检查特定字符串是否包含在其中。