【问题标题】:VTL Script - ForEach loop through arrayVTL 脚本 - ForEach 循环遍历数组
【发布时间】:2021-10-11 13:26:05
【问题描述】:
#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 循环,但在执行一些其他代码(未发布)之前对 $goodSystems 数组进行 $project 变量检查。这可能吗?提前致谢。

【问题讨论】:

    标签: foreach velocity vtl


    【解决方案1】:

    为什么不干脆做:

    #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
    

    【讨论】:

      猜你喜欢
      • 2013-12-16
      • 1970-01-01
      • 2010-10-24
      • 2018-07-17
      • 2017-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多