VELOCITY Array operation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.