gpt4 book ai didi

coldfusion - 检查结构中是否存在键

转载 作者:行者123 更新时间:2023-12-02 15:33:49 26 4
gpt4 key购买 nike

我有一个名为 #cfData# 的变量,它包含一个结构数组。从图中可以清楚地看出,第一个结构数组有 6 个键,第二个只有两个键,即日期和打开。

Dump of Structure

如果我运行一个公共(public)循环,遍历每个键,我将在第二个数组元素处得到一个错误。因此,只有当结构中存在所有键时,以下内容才有效:

<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>

在线阅读后,我想到了使用 StructKeyExists,我认为我可以按以下方式进行:

<cfif structKeyExists(cfData,"bounce")>
<cfoutput>Bounce:#cfData.bounce#"/></cfoutput>
<cfelse>
<cfoutput> Bounce : [none]<br/></cfoutput>
</cfif>

但是我想知道,我到底应该把上面的代码插入cfloop的什么地方呢?如果我的方法有误,请指教。

更新:

谢谢大家。我根据答案使用以下代码运行它,并且运行良好:

<cfloop array="#cfData#" index="i">
<cfif structKeyExists(i, "date")>
<cfset counter++>
<cfoutput>#counter#</cfoutput> Date is: <cfoutput> #i.date#</cfoutput> <br/>
</cfif>
</cfloop>

最佳答案

您不需要“公共(public)循环”。您可以使用

遍历每个结构
<cfloop array="#cfData#" index="i">
<cfloop collection="#i#" item="key">
struct with key '#key#' has data: #i[key]#
</cfloop>
</cfloop>

如果您需要确定结构是否具有特定键,请执行以下操作:

<cfloop array="#cfData#" index="i">
<cfif structKeyExists(i, "someKey")>
<cfset counter++>
</cfif>
</cfloop>

关于coldfusion - 检查结构中是否存在键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21439794/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com