javaarraysvelocityapache-velocityvelocity-template-language

Remove certain element from array in Velocity Template Language (VTL)


I would like to remove a certain element from an array in Velocity Template Language. I did not find any appropriate method looking through the documentation of Apache VTL, that's why I am asking here for help. I have tried following (.remove() doesn't seem to be a method on array items):

#set($linkedWIARRAY = ["ABC-123, DEF-345, GHI-678"])

#set($dummy=$linkedWIARRAY.add("JKL-901"))

#set($dummy = $linkedWIARRAY.remove("DEF-345"))

$linkedWIARRAY

$linkedWIARRAY returns [ABC-123, DEF-345, GHI-678, JKL-901], showing that remove very likely doesn't exist as method on arrays ;)

There is a similar question on SO, that didn't help me: velocity template drop element from array


Solution

  • The problem lies in the initialization of the list. It should be:

    #set($linkedWIARRAY = ["ABC-123", "DEF-345", "GHI-678"])
    

    that is, each string should be enclosed in double quotes, not the whole string.