在 Grails 中的 every() 的最后一项上运行命令
我有一组“任务”,我从正在迭代的 JSON 响应中返回这些“任务”,并对每个任务进行一些处理。这是一些伪代码:
def tasks = grails.converters.JSON.parse(json response)
tasks.each() {task ->
//do some processing here
}
在列表中的最后一个任务中,我想运行一个附加操作。我正在寻找一种内置的方法来在 grails/groovy 中执行此操作。到目前为止,我的搜索还没有结果。
I have an array of 'tasks' that I am getting back from a JSON response that I am iterating through, and doing some processing on each task. Here is some psudocode:
def tasks = grails.converters.JSON.parse(json response)
tasks.each() {task ->
//do some processing here
}
On the very last task in the list, I want to run an additional operation. I'm looking for a built in way to do this in grails/groovy. My searches have thus far yielded no results.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
另一种可能的解决方案是
Another possible solution is
另一种方法可能是执行以下操作:
当然,如果
tasks
列表中只有一个元素,那么它将同时应用两种处理(并且如果tasks
列表中没有元素)列表,它会崩溃):-/编辑
另一种选择(可能不被认为易于阅读)如下:
运行此命令后,
输出
等于:这适用于只有一项的任务列表
Another method might be to do something along the lines of:
Of course, if there's only one element in the
tasks
list, then it will get both processing applied to it (and if there are no elements in the list, it will crash) :-/edit
An alternative (which may not be considered as easily readable), is as follows:
After running this,
output
is equal to:And this works with task lists with just one item
我认为你需要的是:(
提供的tasks.size()> 0)。
[-1] 语法表示列表中的最后一个。
I think what you need is along the lines of:
(provided tasks.size() > 0).
The [-1] syntax means last in the list.