我已经坚持了一段时间了。我正在尝试遍历这个数组,所以我可以执行一些计算,但我无法弄清楚如何循环这些值。有什么建议吗?
我设法弄清楚如何到达那里的集合结构,但我想循环遍历每个结构并抓住那些值,这就是我所坚持的。
此外,我想尽可能避免使用cfscript,因为我还处于学习coldfusion的学习阶段。
这是我的代码:
<cfset houseStuff = {
Bedroom = [
'Luxury Duvet Set with Alternative Down Comforter',
'Accent Coverlet & Shams',
'Two Sets of Luxurious Liens',
'Mattress Pad',
'Blanket',
'Six Bed Pillows',
'Clock Radio',
'Twenty Hangers'
],
Bathroom = [
'Four Bath Towels',
'Four Hand Towels',
'Four Face Towels',
'Bath Rug',
'Shower Curtain',
'Stainless Tooth Brush Holder & Soap Dish',
'Wastebasket',
'Artwork',
'Hair Dryer',
'Toilet Brush & Plunger'
],
Dining = [
'Dinnerware',
'Place Mats',
'Napkins',
'Flatware',
'Glassware & Wine Glasses'
],
Kitchen = [
'Microwave',
'Cookware',
'Mixing Bowls',
'Baking Dish',
'Colander',
'Stainless Utensil Holder',
'Large Fork',
'Large Spoon',
'Spatula',
'Whisk',
'Measuring Spoon & Cup',
'Carving & Paring Knives',
'Four Steak Knives',
'Cutting Board',
'Salt & Pepper Set',
'Wine Opener',
'Coffee Maker',
'Toaster',
'Electric Can Opener',
'Flatware Tray',
'Kitchen Wastebasket',
'Dish Towels',
'Pot Holders',
'Pitcher',
'10" Non-Stick Frying Pan',
'Cookie Sheet',
'Stainless Steel Electric Tea Kettle',
'3 Piece Non-Metal (Spatula, Spoon, Paste Spoon) Combo'
],
Micellaneous = [
'Iron & Cutting Board',
'Cordless Dual Phone with Digital Answering Machine',
'Broom',
'Dust Pan',
'Vacuum',
'Decor',
'Laundry Basket'
],
StarterKit = [
'Bath Tissue',
'Soap',
'Shampoo & Conditioner',
'Paper Towels',
'Sponge',
'Laundry Soap',
'Dishwasher Detergent',
'Liquid Dish Soap',
'Extra Light Bulbs',
'Coffee',
'Sugar',
'Creamer',
'Bottled Water',
'Oatmeal',
'Breakfast Bars',
'Peanuts',
'Chips',
'Mints',
'Welcome Information'
],
MasterBedroom = [
'Queen bed',
'Headboard',
'Two Nightstands',
'Dresser & Mirrior',
'Two Lamps',
'Artwork',
'LCD Television'
],
LivingRoom = [
'Sofa',
'Chair',
'End Table',
'Coffee Table',
'Lamp',
'LCD TV w/stand',
'DVD Player',
'Artwork'
],
DiningRoom = [
'Dining Table',
'Dining Chairs',
'Artwork'
],
OfficePackage = [
'Desk',
'Chair',
'Lamp'
],
AdditionalBedrooms = [
'Queen or Two Twin Beds',
'Headboard',
'Nightstand',
'Chest of Drawers',
'Lamp',
'Artwork'
]
} />
<cfloop collection="#houseStuff#" item="key">
<cfdump var="#key#"> <br>
<!--- <p style="color:##fff;">#key#:</p> <br /> --->
</cfloop>
答案 0 :(得分:3)
没关系,我终于明白了。我必须首先遍历集合。一旦我这样做,在它内部创建另一个循环来循环它的结构化值。
<cfloop collection="houseStuff" item="key">
<!---<cfdump var="#houseStuff[key]"> --->
<cfloop from="1" to="#arrayLen(houseStuff[key])#" index="j">
#j#
</cfloop>
</cfloop>
答案 1 :(得分:1)
我知道你说你更喜欢标签而不是脚本,但如果你处于ColdFusion的学习阶段,我仍然建议学习如何正确使用cfscript
。除了让你的CF更清洁之外,它还会让你的生活变得更轻松,特别是对于像循环这样的事情。
输出所有元素变为:
<cfscript>
for ( i in houseStuff ) { // loop over the outer Structure
writeOutput(i & ":<br>") ;
for ( j in houseStuff[i] ) { // loop over each inner Array key
writeOutput(j & "<br>") ;
}
writeOutput("<br>");
}
</cfscript>
https://trycf.com/gist/898988f6969a57aa5dece39c42037cfd/acf?theme=monokai
...在这种情况下,它确实进入了关于是否在标签或脚本中编写输出代码的哲学讨论,并略微超出了这个问题的范围。但我一直是学习最佳实践的支持者,同时也是基础知识。就个人而言,我确实倾向于遵循tag-for-output视图,但对于基本循环,脚本版本对我来说有点清晰。我会学到两者。
另请查看:http://www.learncfinaweek.com。有两种方法的循环中有一节。