Common methods:
$myarray. IsEmpty () array is empty
$myarray. Size () Gets the number of array elements
$myarray. Get (2) gets an array of elements that specify subscript
$myarray. Add () add element
Arrays & Access
Velocity accesses an Array object and cannot access elements of a particular location through similar arr[2].
#set ($arr = [0, 1, 2, 3])
$arr. Get (2)
Note: The array in Velocity corresponds to the list object in Java. For Java native Array objects, you can only traverse #foreach, and you cannot use $arr [0] and $arr. Get (0) method.
Scope (range)
#foreach ($item in [10..20])
$item
#end
Objects & Access
#set ($obj = {"Key": "Value", "Name": "Space"})
$obj. Get ("key")
#foreach (#item in $obj)
$item
#end
The above $item takes values, and you can use the EntrySet () or KeySet () method if you need to take the keys of the object while traversing the object.
#foreach ($item in $!obj.entryset ())
$!item.key: $!item.value
#end
#foreach ($item in $obj. KeySet ())
$item: $obj. Get ($item)
#end
Note: The traversal of this collection is unordered, that is, the traversal order may be different from the order in which elements are defined in the $obj (as far as is known, it is sorted by the letter of the key).
There are two other imperfect solutions:
i:
#set ($obj = [
{"Key": "Key", "value": "Value"},
{"key": "Name", "Value": "Space"}
])
#foreach ($item In $obj)
$item. Key: $item. Value
#end
II:
#set ($obj = [
["Key", "value"],
["Name", "Space"]
])
#foreach ($item in $obj)
$item. Get (0): $item. Get (1)
#end
is imperfect because: for a known key, it can be directly
$obj. Get ("key")
It is now only possible to traverse and compare, and the earlier Velocity version cannot use #break to exit the loop immediately after a match has been found.
#foreach ($item in $obj)
#if ("key" = = $!obj.get (0))
#set ($myKey = $!type.get (1))
# #break
#end
#end
more information can be found in the following:
Http://wiki.hotoo.me/Velocity-Notes.html
Http://wiki.hotoo.me/Velocity.html
VELOCITY Array operation