ColdFusion 循环遍历列表的[仅部分]
真的很难找到一种方法来循环冷融合列表的一部分(或者一半?)。我设置了一个 if 语句来检查列表的长度以及是否超过 30...我想将列表拆分为前 30 个和其余部分?但不确定这是否是最好的解决方案。我真的不需要太多细节,我相信我自己能弄清楚这么多,我更希望被指出正确的方向......
Really struggling with finding a way to loop through only part (or half maybe?) of a coldfusion list. I've got an if statement set up to check the length of the list and if it is over 30... I want to split the list into the first 30 and the remainder? Not sure if that's the best solution though. I really don't need much detail I'm sure I can figure that much out myself I am more looking just to be pointed in the right direction...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不要循环列表,而是从 1 循环到数字,并在循环中使用 listGetAt()。对于列表的其余部分,只需从#myvar + 1# 循环到#listLen#。
当然,这不是最有效的方法。如果遇到性能问题,可能需要通过 listToArray() 将列表转换为数组,然后执行以下操作:
Rather than looping over the list, loop from 1 to a number, and use listGetAt() in the loop. For the remainder of the list, just loop from #myvar + 1# to #listLen#.
Granted, it's not the most efficient method. If you encounter performance issues, might want to convert the list to an array via listToArray(), and then do:
您可以利用底层的 java 函数。
将为您提供一个数组“testChunk”,其中包含列表中的前 30 个项目。您现在可以轻松地循环数组的元素。
为了更清楚地说明这一点,下面是一个示例:
You can make use of the underlying java functions.
will give you an array "testChunk" with the first 30 items in the list. You can now easily loop over the elements of the Array.
To make this more clear, here is an example:
这实际上取决于您想要完成的任务、如何使用这两个独立的数据集以及列表中的数据类型......数据真的需要分离吗?你可以吗:
如果你不需要采取额外的步骤来分成两个列表或数组,那么就可以节省一些编码和工作量。执行时间
另外 - 如果您的起点发生变化,您始终可以:
-sean
it really depends on what you are trying to accomplish, how the 2 separated sets of data will be used and whst type of data you have in the list... does the data really need to be separated? can you just:
If you don't need to take the extra step of separating into 2 lists or arrays, save your self some coding & execution time
Also - if your starting point changes, you can always:
-sean
在 CF10 或 Railo 4 中,您可以使用 first() 和 rest() 函数来自 Underscore.cfc 来拆分您的列表:
_.first()
和_.rest()
都根据传递给它们的索引返回新数组。这些方法只是委托给本机arraySlice()
函数,但它们可以帮助您编写更具表现力的代码。注:我写的是Underscore.cfc
In CF10 or Railo 4, you could use the first() and rest() functions from Underscore.cfc to split up your list:
_.first()
and_.rest()
both return new arrays based on the index passed into them. The methods simply delegate to the nativearraySlice()
function, but they can help you write more expressive code.Note: I wrote Underscore.cfc