Search code examples
coldfusion

in Coldfusion how do I add a 'column' to the struct inside an array?


For example I have array ads and inside it has images

ads
   images
      url = someurl
      title = sometitle
      *here I want to add prop = someprop 
   images
      url = someurl
      title = sometitle
      *here I want to add prop = someprop
ads
   images
      url = someurl
      title = sometitle
      *here I want to add prop = someprop

Now I loop

<cfloop array="ads" index="i">
   <cfloop array="i.images" index="j">
      <cfif somecondition>
         <cfset prop = 'yes'>
      <cfelse>
         <cfset prop = 'no'>
      </cfif>
      <cfset ArrayAppend(i.images[j],prop)> ???
   </cfloop>
</cfloop>

I tried several things with the ArrayAppend, tried a StructAppend, but apparently I am unable to understand how I would go about.


Solution

  • You were close. You can simply set the prop on the item in the images loop. This is assuming that the items in the i.images array are structures.

    <cfloop array="ads" index="i">
       <cfloop array="i.images" index="j">
          <cfif somecondition>
             <cfset j.prop = 'yes'>
          <cfelse>
             <cfset j.prop = 'no'>
          </cfif>
       </cfloop>
    </cfloop>