foreach - VTL 脚本 - ForEach 循环遍历数组
问题描述
#set ($goodSystems = ["one_system","two_system"....])
#foreach ($system in $all.Systems) ##this code must remain the same
#if ($project == "one_system")
如果上下文难以理解,我们深表歉意,因为这在很大程度上已经过清理。我会尽量让它有意义。此代码的#foreach 部分无法更改,因为这是循环访问数据库中所有系统的唯一方法。有些系统我不需要访问因此#if 语句。我一直在使用 if 语句并手动更改 $project 变量,但这会非常耗时。
我正在寻找一种仍然使用#foreach 循环的方法,但在执行其他一些代码(未发布)之前让 $project 变量对 $goodSystems 数组进行检查。这可能吗?提前致谢。
解决方案
为什么不简单地做:
#if( $goodSystems.contains($system) )
当然,这是一个快速而懒惰的解决方案,纯粹主义者可能会说,最佳方式是仅在目标元素上循环,必要时拆分循环,如下所示:
#set( $all = { 'Systems': ["one","two","three"] } ) ## testcase init
#set( $good = ["one","two"] ) ## manual input
#set( $junk = $good.retainAll($all) ) ## filter out manual input
#set( $notgood = $all.Systems )
#set( $junk = $notgood.removeAll($good)) ## systems with are not good
#foreach( $system in $good )
... ## do something for good systems
#end
#foreach( $system in $all.systems )
... ## do something for all systems
#end
#foreach( $system in $notgood )
... ## do something for systems which aren't good
#end
推荐阅读
- javascript - 为什么使用 D3.min 版本会破坏此代码?
- ios - CIKernel 的意外行为
- c# - Asp.NET C# MVC - 如何显示结果与多个表的查询
- python - python 3素数错误答案产生为什么这段代码给我错误的素数,如9,21,.......?
- sql - 无法确定连接查询
- python - MyPy 无法识别修饰函数的返回类型
- javascript - Javascript - 通过其名称和存储在变量中的参数调用函数,(理想情况下没有`eval`,但如果只有这样就可以了)
- python - Discord.py 如何让命令忽略参数
- java - 实现抽象类的类的类型与抽象类的泛型类型不兼容
- args - For循环不循环通过args