javaloopsvelocityvtlapache-velocity

How to check if value exists in an array using Velocity Template Language


I am not sure how I could achieve the following using Velocity Template Language.

Essentially, I'd like to check if a given value exists from the given list/array. In C#, it's equivalent to .Any() or .Contains() method.

#set($myValues = ["apple", "banana", "strawberry"])
#foreach($i in $items)
    $myValues.any($i) <= better way to achieve this??
    or something like this
    $myValues.contains($i)
#end

Of course, I could just use multiple if...else... conditions and loop through the list to check the existence of a certain value, but the code gets messy very quickly (if a lookup list is huge). Is there any better way to do this?


Solution

  • $myValues.contains($i)
    

    or

    $list.contains($myValues, $i)
    

    (this one is deprecated since the former was introduced in Velocity 1.6)