问题描述
我想测试:
<cfif Exists(MyStruct["mittens"])>
</cfif>
如果mittens键不存在于MyStruct中,它会返回什么? 0或??
If the "mittens" key doesn't exist in MyStruct, what will it return? 0, or ""??
应该替换Exists函数?
What should replace Exists function?
UPDATE
我试过,
<cfif IsDefined(MyStruct.mittens)>
这也会引发错误
推荐答案
我建议:
<cfif StructKeyExists(MyStruct, "mittens")>
<!--- or --->
<cfset key = "mittens">
<cfif StructKeyExists(MyStruct, key)>
幕后,这会调用 containsKey()
java.util.map的方法ColdFusion结构体是基于。
Behind the scenes this calls the containsKey()
method of the java.util.map the ColdFusion struct is based on. This is arguably the fastest method of finding out if a key exists.
可选择的是:
<cfif IsDefined("MyStruct.mittens")>
<!--- or --->
<cfset key = "mittens">
<cfif IsDefined("MyStruct.#key#")>
$ b StructKeyExists()慢。在正面:您可以在一个调用中测试嵌套结构中的子键:
Behind the scenes this calls Eval()
on the passed string (or so I believe) and tells you if the result is a variable reference. In comparison this is slower than StructKeyExists()
. On the plus side: You can test for a sub-key in a nested structure in one call:
<cfif IsDefined("MyStruct.with.some.deeply.nested.key")>
这篇关于如何测试一个变量是否存在于ColdFusion结构中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!