for-in
is the statement for loop with collection. Basic syntax is as below.
var
for declaration of a variable is not required when you want to use the variable which is already declared at the outside scope.
for (var e in collection) {
...
}
The following objects can be used at the collection
part.
- Range object.
- Array object.
- Key-Value object.
See examples below about how to work with each object above.
for (var e in 2..10) {
System.println(e);
}
2
3
4
5
6
7
8
9
10
for (var e in 2...10) {
System.println(e);
}
2
3
4
5
6
7
8
9
for (var e in [2,3,4,5,6,7,8,9,10]) {
System.println(e);
}
2
3
4
5
6
7
8
9
10
for ([i, j] in [[1,2], [3,4], [5,6]]) {
System.println("[%{i}, %{j}]");
}
[1, 2]
[3, 4]
[5, 6]
for ([i, j] in [1, 2, 3]) {
System.println([i, j]);
}
Note that the result is NOT [1, 2]
and [3, null]
, against your expected.
[1, null]
[2, null]
[3, null]
var obj = { a: 10, b: 100 };
for ([key, value] in obj) {
System.println("key: %{key} => value: %{value}");
}
key: a => value: 10
key: b => value: 100