我有一个名为变量的变量#cfData#,其中包含一组结构.从图像中可以清楚地看到,对于第一个结构阵列,有6个键,对于第二个,只有两个键,即日期和打开.

如果我运行一个公共循环,要遍历每个键,我将在第二个数组元素处得到一个错误.因此,只有当结构中存在所有键时,以下内容才有效:
<cfset blockedtotal = 0 />
<cfset bouncetotal = 0 />
<cfset blocked = 0/>
<cfset datetotal = 0 />
<cfloop array = #cfData# index = "i">
<cfset blockedtotal += i.blocked />
<cfset bouncetotal += i.bounce />
</cfloop>
Run Code Online (Sandbox Code Playgroud)
在线阅读后,我想到了使用StructKeyExists,我认为我可以通过以下方式继续:
<cfif structKeyExists(cfData,"bounce")>
<cfoutput>Bounce:#cfData.bounce#"/></cfoutput>
<cfelse>
<cfoutput> Bounce : [none]<br/></cfoutput>
</cfif>
Run Code Online (Sandbox Code Playgroud)
但我想知道,我应该在cfloop中插入上面的代码到底在哪里?请告知我的方法是否错误.
更新:
多谢你们.我根据答案使用以下代码运行它并运行良好:
<cfloop array="#cfData#" index="i">
<cfif structKeyExists(i, "date")>
<cfset counter++>
<cfoutput>#counter#</cfoutput> Date is: <cfoutput> #i.date#</cfoutput> <br/>
</cfif>
</cfloop>
Run Code Online (Sandbox Code Playgroud)
你不需要"共同循环".您可以使用循环遍历每个结构
<cfloop array="#cfData#" index="i">
<cfloop collection="#i#" item="key">
struct with key '#key#' has data: #i[key]#
</cfloop>
</cfloop>
Run Code Online (Sandbox Code Playgroud)
当然,如果您需要确定结构是否具有某个键,请执行以下操作:
<cfloop array="#cfData#" index="i">
<cfif structKeyExists(i, "someKey")>
<cfset counter++>
</cfif>
</cfloop>
Run Code Online (Sandbox Code Playgroud)