Coldfusion—如何循环遍历结构数组并动态输出所有键值?

时间:2021-03-01 20:46:16

Giving the Array of Structure below:

给出如下结构数组:

Coldfusion—如何循环遍历结构数组并动态输出所有键值?

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>

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

我不是一个ColdFusion的家伙,我只是在帮助一个朋友,而ColdFusion的语法与。net非常不同:-)

Thank you for your help

谢谢你的帮助。

1 个解决方案

#1


18  

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

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

或者您可以使用CFScript,它应该更容易获取。

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

use this link: http://www.learncfinaweek.com/week1/Looping/

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

#1


18  

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

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

或者您可以使用CFScript,它应该更容易获取。

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

use this link: http://www.learncfinaweek.com/week1/Looping/

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