Coldfusion - 如何循环通过结构数组,并动态打印所有KEY值? [英] Coldfusion - How to loop through an Array of Structure and print out dynamically all KEY values?

查看:197
本文介绍了Coldfusion - 如何循环通过结构数组,并动态打印所有KEY值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

提供结构数组:

我可以通过执行以下操作打印所有字段的所有值:

I am able to print out all values from all fields by doing:

    <cfset ColumnNames  = structKeyArray(ApiData[1])>                       
    <cfset ColumnLength = ArrayLen(ColumnNames)>    

    <cfloop from="1" to="#ArrayLen(ApiData)#" index="i">            
       <cfdump var="#ApiData[i].Created#">              
       <cfdump var="#ApiData[i].Name#">
               ...and so on

现在我试图遍历所有字段我不必实际写入每个字段的
名称。我如何动态地这样做?
类似:

Now I am trying to loop through all fields so that I dont have to actually write the name of each field. How do I do this dynamically? Something like:

    <cfloop from="1" to="#ArrayLen(ApiData)#" index="i">    
      <cfloop from="1" to="#ColumnLength#" index="i">
              <!---<cfdump var="#ApiData[i]." + "#ColumnNames[i]#" + "#">--->
              <!---<cfdump var="#ApiData[i].ColumnNames[i]#">--->
      </cfloop>
    </cfloop>

我不是ColdFusion的家伙,只是帮助一个好友,ColdFusion语法与.Net : - )

I am not a ColdFusion guy, just helping a buddy and the ColdFusion syntax is very different from .Net :-)

感谢您的帮助

推荐答案

<cfloop from="1" to="#arrayLen(ApiData)#" index="i">
  <cfset data = ApiData[i]>
  <cfloop collection="#data#" item="key">
    #key#:#data[key]#
  </cfloop> 
</cfloop>

或者你可以使用CFScript,这应该更容易拿起。

Or you can use CFScript, which should be much easier to pick up.

for (d in ApiData)  // for-in loop for array
{
  for (key in d)  // for-in loop for struct
  {
     writeOutput(key & ":" & d[key]);
  }
}

使用此链接: http://www.learncfinaweek.com/week1/Looping/

这篇关于Coldfusion - 如何循环通过结构数组,并动态打印所有KEY值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆