I have an array called device
, which looks like this (simplified):
label : "Device 1",
exhibits : [{
item : 1,
desc : "This is a sample"
},{
item : 2,
desc : "This is another sample"
},{
item : 3,
desc : "This is a third"
}]
I'm trying to print exhibits
neatly for a PDF, so I'm thinking comma-deliniated like this:
1, 2, 3
This is my code:
<cfloop array="#device.exhibits#" index="exhibit">
#exhibit.item#
</cfloop>
But I get this:
123
Yes, I could manually figure out if there should be commas or not, but is there a better way to do this?
The usual approach is to extract the data first:
<!--- extract the itemNumber of every exhibit --->
<cfset itemNumberList = []>
<cfloop array="#device.exhibits#" index="exhibit">
<cfset itemNumberList.add(exhibit.itemNumber)>
</cfloop>
And then we transform the extracted data to a comma-separated list (string):
<cfset itemNumberList = arrayToList(itemNumberList, ", ")>
<!--- 1, 2, 3 --->
<cfoutput>#itemNumberList#</cfoutput>
Array-mapping (see Shawn's answer) is a more fancy (readable?) way.